From 5cff4999475e4087a4a66944db8e54769a1de760 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Mon, 15 Jan 2024 23:26:01 +0000 Subject: [PATCH 01/20] recover PR --- .../components/external_eeprom/__init__.py | 42 ++ .../external_eeprom/external_eeprom.cpp | 369 ++++++++++++++++++ .../external_eeprom/external_eeprom.h | 79 ++++ 3 files changed, 490 insertions(+) create mode 100644 esphome/components/external_eeprom/__init__.py create mode 100644 esphome/components/external_eeprom/external_eeprom.cpp create mode 100644 esphome/components/external_eeprom/external_eeprom.h diff --git a/esphome/components/external_eeprom/__init__.py b/esphome/components/external_eeprom/__init__.py new file mode 100644 index 0000000000..ad764debe9 --- /dev/null +++ b/esphome/components/external_eeprom/__init__.py @@ -0,0 +1,42 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import i2c +from esphome.const import CONF_ID + +CODEOWNERS = ["@pebblebed-tech"] +DEPENDENCIES = ["i2c"] +MULTI_CONF = True + +CONF_EE_MEMORY_SIZE = "ee_memory_size" +CONF_EE_PAGE_SIZE = "ee_page_size" +CONF_EE_PAGE_WRITE_TIME = "ee_page_write_time" +CONF_I2C_BUFFER_SIZE = "i2c_buffer_size" + +ext_eeprom_component_ns = cg.esphome_ns.namespace("external_eeprom") +ExtEepromComponent = ext_eeprom_component_ns.class_( + "ExtEepromComponent", cg.Component, i2c.I2CDevice +) + +CONFIG_SCHEMA = ( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(ExtEepromComponent), + cv.Required(CONF_EE_MEMORY_SIZE): cv.uint32_t, + cv.Required(CONF_EE_PAGE_SIZE): cv.uint16_t, + cv.Required(CONF_EE_PAGE_WRITE_TIME): cv.uint8_t, + cv.Required(CONF_I2C_BUFFER_SIZE): cv.uint8_t, + } + ) + .extend(cv.COMPONENT_SCHEMA) + .extend(i2c.i2c_device_schema(0x57)) +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await i2c.register_i2c_device(var, config) + cg.add(var.set_memory_size(config[CONF_EE_MEMORY_SIZE])) + cg.add(var.set_page_size(config[CONF_EE_PAGE_SIZE])) + cg.add(var.set_page_write_time(config[CONF_EE_PAGE_WRITE_TIME])) + cg.add(var.set_i2c_buffer_size(config[CONF_I2C_BUFFER_SIZE])) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp new file mode 100644 index 0000000000..67ee3684f4 --- /dev/null +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -0,0 +1,369 @@ +#include "external_eeprom.h" + +namespace esphome { +namespace external_eeprom { + +static const char *const TAG = "external_eeprom"; + +void ExtEepromComponent::setup() { + if (!this->is_connected(this->address_)) { + ESP_LOGE(TAG, "Device on address 0x%x not found!", this->address_); + this->mark_failed(); + } else { + ESP_LOGE(TAG, "Memory detected!"); + } +} + +void ExtEepromComponent::loop() {} + +void ExtEepromComponent::dump_config() { + ESP_LOGCONFIG(TAG, "External Eeprom"); + LOG_I2C_DEVICE(this); + ESP_LOGCONFIG(TAG, "Size = %d", this->get_memory_size()); + ESP_LOGCONFIG(TAG, "Page size = %d", this->get_page_size()); + ESP_LOGCONFIG(TAG, "I2C HW buffer size = %d", this->get_i2c_buffer_size()); + ESP_LOGCONFIG(TAG, "Page write time = %d", this->get_page_write_time()); + if (this->is_failed()) { + ESP_LOGE(TAG, "Communication with device failed!"); + } +} +/// @brief This checks whether the device is connected and not busy +/// @param Caller can pass in an 0xFF I2C address. This is helpful for larger EEPROMs that have two addresses (see block +/// bit 2). +/// @return an boolean True for connected +bool ExtEepromComponent::is_connected(uint8_t i2c_address) { + i2c::ErrorCode err; + if (i2c_address == 255) + i2c_address = this->address_; + err = this->bus_->write(i2c_address, nullptr, 0, true); + if (err != i2c::ERROR_OK) + ESP_LOGE(TAG, "EEPROM not connected and raise this error %d", err); + return (err == i2c::ERROR_OK); +} + +/// @brief Reads a byte from a given location +/// @param memaddr is the location to read +/// @return the byte read from device +uint8_t ExtEepromComponent::read8(uint32_t memaddr) { + uint8_t temp_byte; + this->read(memaddr, &temp_byte, 1); + return temp_byte; +} +/// @brief Reads a 16 bit word from a given location +/// @param memaddr is the location to read +/// @return the word read from the device +uint16_t ExtEepromComponent::read16(uint32_t memaddr) { + uint16_t val; + this->read(memaddr, (uint8_t *) &val, sizeof(uint16_t)); + return val; +} +/// @brief Reads a 32 bit word from a given location +/// @param memaddr is the location to read +/// @return the word read from the device +uint32_t ExtEepromComponent::read32(uint32_t memaddr) { + uint32_t val; + this->read(memaddr, (uint8_t *) &val, sizeof(uint32_t)); + return val; +} +/// @brief Reads a float from a given location +/// @param memaddr is the location to read +/// @return the float read from the device +float ExtEepromComponent::read_float(uint32_t memaddr) { + float val; + this->read(memaddr, (uint8_t *) &val, sizeof(float)); + return val; +} +/// @brief Reads a double from a given location +/// @param memaddr is the location to read +/// @return the double read from the device +double ExtEepromComponent::read_double(uint32_t memaddr) { + double val; + this->read(memaddr, (uint8_t *) &val, sizeof(double)); + return val; +} +/// @brief Bulk read from the device +/// @note breaking up read amt into 32 byte chunks (can be overriden with setI2Cbuffer_size) +/// @note Handles a read that straddles the 512kbit barrier +/// @param memaddr is the starting location to read +/// @param buff is the pointer to an array of bytes that will be used to store the data received +/// @param buffer_size is the size of the buffer and also the number of bytes to be read +void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_size) { + ESP_LOGVV(TAG, "Read %d bytes from address %d", buffer_size, memaddr); + uint32_t size = buffer_size; + uint8_t *p = buff; + while (size >= 1) { + // Limit the amount to read to a page size + uint16_t amt_to_read = size; + if (amt_to_read > i2c_buffer_size_) // I2C buffer size limit + amt_to_read = i2c_buffer_size_; + + // Check if we are dealing with large (>512kbit) EEPROMs + uint8_t i2c_address = this->address_; + if (this->get_memory_size() > 0xFFFF) { + // Figure out if we are going to cross the barrier with this read + if (memaddr < 0xFFFF) { + if (0xFFFF - memaddr < amt_to_read) // 0xFFFF - 0xFFFA < I2C_buffer_size + amt_to_read = 0xFFFF - memaddr; // Limit the read amt to go right up to edge of barrier + } + + // Figure out if we are accessing the lower half or the upper half + if (memaddr > 0xFFFF) + i2c_address |= 0b100; // Set the block bit to 1 + } + i2c::ErrorCode ret; + if (this->get_memory_size() > 2048) { + uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; + ret = this->bus_->write(this->address_, maddr, 2, false); + } else { + uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; + ret = this->bus_->write(this->address_, maddr, 1, false); + } + if (ret != i2c::ERROR_OK) { + ESP_LOGE(TAG, "Read raise this error %d on setting address", ret); + } + ESP_LOGVV(TAG, "Read - Done Set address, Ammount to read %d", amt_to_read); + ret = this->bus_->read(this->address_, p, amt_to_read); + if (ret != i2c::ERROR_OK) { + ESP_LOGE(TAG, "Read raised this error %d on reading data", ret); + } + ESP_LOGVV(TAG, "Done Read"); + memaddr += amt_to_read; + p += amt_to_read; + size -= amt_to_read; + } +} +/// @brief Read a std::string from the device +/// @note It write the string with an extra byte containing the size at the memaddr +/// @note It is limited to reading a max length of 254 bytes +/// @param memaddr is the starting location to read +/// @param str_to_read will hold the bytes read from the device on return of the fuction + +uint32_t ExtEepromComponent::read_string_from_eeprom(uint32_t memaddr, std::string &str_to_read) { + uint8_t new_str_len = read8(memaddr); + uint8_t data[new_str_len + 1]; + read(memaddr + 1, (uint8_t *) data, new_str_len); + data[new_str_len] = '\0'; + str_to_read = (char *) data; + return memaddr + 1 + new_str_len; +} +/// @brief Writes a byte to a given location +/// @note It will check first to see if loccation already has the value to protect write cycles +/// @param memaddr is the location to write +/// @param data_to_write contains the byte to be written +void ExtEepromComponent::write8(uint32_t memaddr, uint8_t data_to_write) { + if (read8(memaddr) != data_to_write) { // Update only if data is new + write(memaddr, &data_to_write, 1); + } +} +/// @brief Writes a 16 bit word to a given location +/// @note It will check first to see if loccation already has the value to protect write cycles +/// @param memaddr is the location to write +/// @param value contains the word to be written +void ExtEepromComponent::write16(uint32_t memaddr, uint16_t value) { + if (read16(memaddr) != value) { // Update only if data is new + uint16_t val = value; + this->write(memaddr, (uint8_t *) &val, sizeof(uint16_t)); + } +} +/// @brief Writes a 32 bit word to a given location +/// @note It will check first to see if loccation already has the value to protect write cycles +/// @param memaddr is the location to write +/// @param value contains the word to be written +void ExtEepromComponent::write32(uint32_t memaddr, uint32_t value) { + if (read32(memaddr) != value) { // Update only if data is new + uint32_t val = value; + this->write(memaddr, (uint8_t *) &val, sizeof(uint32_t)); + } +} +/// @brief Writes a float to a given location +/// @note It will check first to see if loccation already has the value to protect write cycles +/// @param memaddr is the location to write +/// @param value contains the float to be written +void ExtEepromComponent::write_float(uint32_t memaddr, float value) { + if (read_float(memaddr) != value) { // Update only if data is new + float val = value; + this->write(memaddr, (uint8_t *) &val, sizeof(float)); + } +} +/// @brief Writes a double to a given location +/// @note It will check first to see if loccation already has the value to protect write cycles +/// @param memaddr is the location to write +/// @param value contains the double to be written +void ExtEepromComponent::write_double(uint32_t memaddr, double value) { + if (read_double(memaddr) != value) // Update only if data is new + { + double val = value; + this->write(memaddr, (uint8_t *) &val, sizeof(double)); + } +} +/// @brief Bulk write to the device +/// @note breaking up read amt into 32 byte chunks (can be overriden with setI2Cbuffer_size) +/// @note Handles a write that straddles the 512kbit barrier +/// @param memaddr is the starting location to write +/// @param data_to_write is the pointer to an array of bytes that will be written +/// @param buffer_size is the size of the buffer and also the number of bytes to be written +void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_t buffer_size) { + ESP_LOGVV(TAG, "Write %d bytes to address %d", buffer_size, memaddr); + uint32_t size = buffer_size; + uint8_t *p = data_to_write; + // Check to make sure write is inside device range + if (memaddr + buffer_size >= this->memory_size_bytes_) { + buffer_size = this->memory_size_bytes_ - memaddr; // if not shorten the write to fit + ESP_LOGE(TAG, "Trying write data beyond device size, Address %d", (memaddr + buffer_size)); + } + + uint16_t max_write_size = this->memory_page_size_bytes_; + if (max_write_size > i2c_buffer_size_) + max_write_size = i2c_buffer_size_; + + /// Break the buffer into page sized chunks + + while (size >= 1) { + /// Limit the amount to write to either the page size or the I2C limit + uint16_t amt_to_write = size; + if (amt_to_write > max_write_size) + amt_to_write = max_write_size; + + if (amt_to_write > 1) { + /// Check for crossing of a page line. Writes cannot cross a page line. + uint16_t page_number_1 = memaddr / this->memory_page_size_bytes_; + uint16_t page_number_2 = (memaddr + amt_to_write - 1) / this->memory_page_size_bytes_; + if (page_number_2 > page_number_1) { + amt_to_write = (page_number_2 * this->memory_page_size_bytes_) - + memaddr; /// Limit the write amt to go right up to edge of page barrier + } + } + /// Check if we are dealing with large (>512kbit) EEPROMs + uint8_t i2c_address = this->address_; + if (this->get_memory_size() > 0xFFFF) { + /// Figure out if we are going to cross the barrier with this write + if (memaddr < 0xFFFF) { + if (0xFFFF - memaddr < amt_to_write) /// 0xFFFF - 0xFFFA < I2C_buffer_size + amt_to_write = 0xFFFF - memaddr; /// Limit the write amt to go right up to edge of barrier + } + + /// Figure out if we are accessing the lower half or the upper half + if (memaddr > 0xFFFF) + i2c_address |= 0b100; /// Set the block bit to 1 + } + + ESP_LOGVV(TAG, "Write block %d bytes to address %d", amt_to_write, memaddr); + this->write_block_(memaddr, p, amt_to_write); + memaddr += amt_to_write; + p += amt_to_write; + size -= amt_to_write; + ESP_LOGVV(TAG, "After write size %d amt %d add %d", size, amt_to_write, memaddr); + + delay(this->memory_page_write_time_ms_); /// Delay the amount of time to record a page + } +} + +/// @brief Write a std::string to the device +/// @note It writes the string with an extra byte containing the size at the memaddr eg address 0 +/// @note it is limited to writing a max length of the string of 254 bytes and will trim extra bytes +/// @param memaddr is the starting location to write +/// @param str_to_write contains the std::string to be wriiten +uint32_t ExtEepromComponent::write_string_to_eeprom(uint32_t memaddr, std::string &str_to_write) { + if (str_to_write.length() > 254) { + ESP_LOGE(TAG, "String to long. Limit is 254 chars"); + str_to_write.resize(254); + } + uint8_t len = str_to_write.length(); + const char *p = str_to_write.c_str(); + write8(memaddr, len); + write(memaddr + 1, (uint8_t *) p, len); + return memaddr + 1 + len; +} +void ExtEepromComponent::dump_eeprom(uint32_t start_addr, uint16_t word_count) { + std::vector words; + uint16_t address; + uint16_t data; + std::string res; + char adbuf[8]; + char buf[5]; + size_t len; + address = start_addr; + while (address < (word_count + start_addr)) { + for (size_t i = address; i < (address + 16); i += 2) { + this->read_object(i, data); + words.push_back(data); + } + sprintf(adbuf, "%04X : ", address); + res = adbuf; + len = words.size(); + for (size_t u = 0; u < len; u++) { + if (u > 0) { + res += " "; + } + sprintf(buf, "%04X", words[u]); + res += buf; + } + ESP_LOGD(TAG, "%s", res.c_str()); + words.clear(); + address = address + 16; + } +} + +/// @brief Erase the entire device +/// @note **** to be used carefully, as there is no recovery **** +/// @param value_to_write optional value to be written to all locations defaults to 0x00 +void ExtEepromComponent::erase(uint8_t value_to_write) { + uint8_t temp_buffer[this->memory_page_size_bytes_]; + for (uint32_t x = 0; x < this->memory_page_size_bytes_; x++) + temp_buffer[x] = value_to_write; + + for (uint32_t addr = 0; addr < this->get_memory_size(); addr += this->memory_page_size_bytes_) + write(addr, temp_buffer, this->memory_page_size_bytes_); +} +/// @brief Sets the size of the device in bytes +/// @param memSize contains the size of the device +void ExtEepromComponent::set_memory_size(uint32_t mem_size) { memory_size_bytes_ = mem_size; } +/// @brief Gets the user specified size of the device in bytes +/// @return size in bytes +uint32_t ExtEepromComponent::get_memory_size() { return memory_size_bytes_; } +/// @brief Sets the page size of the device in bytes +/// @param page_size contains the size of the device pages +void ExtEepromComponent::set_page_size(uint8_t page_size) { memory_page_size_bytes_ = page_size; } +/// @brief Gets the user specified size of the device pages in bytes +/// @return Page size in bytes +uint8_t ExtEepromComponent::get_page_size() { return memory_page_size_bytes_; } +/// @brief Sets the page write for the device in ms +/// @param write_time_ms contains the time to write a page of the device +void ExtEepromComponent::set_page_write_time(uint8_t write_time_ms) { memory_page_write_time_ms_ = write_time_ms; } +/// @brief Gets the user specified write time for a device page in ms +/// @return page write time in ms +uint8_t ExtEepromComponent::get_page_write_time() { return memory_page_write_time_ms_; } +/// @brief Sets the hw I2C buffer size -2, as 2 bytes are needed for control & addr +/// @param buffer size in bytes, (ESP devices has a 128 I2C buffer so it is set to 126) +void ExtEepromComponent::set_i2c_buffer_size(uint8_t i2c_buffer_size) { i2c_buffer_size_ = i2c_buffer_size; } +/// @brief Gets the hw I2C buffer size -2, as 2 bytes are needed for control & addr +/// @return buffer size in bytes +uint8_t ExtEepromComponent::get_i2c_buffer_size() { return i2c_buffer_size_; } + +// private functions +void ExtEepromComponent::write_block_(uint32_t memaddr, const uint8_t *obj, uint8_t size) { + i2c::WriteBuffer buff[2]; + i2c::ErrorCode ret; + // Check if the device has two address bytes + if (this->get_memory_size() > 2048) { + uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; + buff[0].data = maddr; + buff[0].len = 2; + buff[1].data = obj; + buff[1].len = size; + ret = this->bus_->writev(this->address_, buff, 2, true); + } else { + uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; + buff[0].data = maddr; + buff[0].len = 1; + buff[1].data = obj; + buff[1].len = size; + ret = this->bus_->writev(this->address_, buff, 2, true); + } + if (ret != i2c::ERROR_OK) { + ESP_LOGE(TAG, "Write raise this error %d on writing data to this address %d", ret, memaddr); + } +} + +} // namespace external_eeprom +} // namespace esphome diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h new file mode 100644 index 0000000000..643a5fff04 --- /dev/null +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -0,0 +1,79 @@ +#pragma once +#include "esphome/core/hal.h" +#include "esphome/core/log.h" +#include "esphome/core/component.h" +#include "esphome/components/i2c/i2c.h" + +namespace esphome { +namespace external_eeprom { + +/// @brief This Class provides the methods to read and write data from an 24 LC/AT XX devices such as 24LC32. See +/// https://ww1.microchip.com/downloads/en/devicedoc/doc0336.pdf + +class ExtEepromComponent : public i2c::I2CDevice, public Component { + public: + void setup() override; + void loop() override; + void dump_config() override; + float get_setup_priority() const override { return setup_priority::BUS; } + + bool is_connected(uint8_t i2c_address = 255); + bool is_busy(uint8_t i2c_address = 255); + + uint8_t read8(uint32_t memaddr); // Read a single byte from address memaddr + uint16_t read16(uint32_t memaddr); + uint32_t read32(uint32_t memaddr); + float read_float(uint32_t memaddr); + double read_double(uint32_t memaddr); + void read(uint32_t memaddr, uint8_t *buff, + uint16_t buffer_size); // Read a buffer of buffersize (bytes) from adress memaddr + uint32_t read_string_from_eeprom(uint32_t memaddr, + std::string &str_to_read); // Read a Std::string from adress memaddr + + void write8(uint32_t memaddr, uint8_t data_to_write); // Write a single byte to address memaddr + void write16(uint32_t memaddr, uint16_t value); + void write32(uint32_t memaddr, uint32_t value); + void write_float(uint32_t memaddr, float value); + void write_double(uint32_t memaddr, double value); + void write(uint32_t memaddr, uint8_t *data_to_write, + uint16_t buffer_size); // Write a buffer of buffersize (bytes) to adress memaddr + uint32_t write_string_to_eeprom(uint32_t memaddr, std::string &str_to_write); // Write a string to adress memaddr + + void dump_eeprom(uint32_t start_addr, uint16_t word_count); // Display the contents of EEPROM in hex to the debug log + void erase(uint8_t value_to_write = 0x00); // Erase the entire memory. Optional: write a given byte to each spot. + + // Getters and Setters for component config + void set_memory_size(uint32_t mem_size); // Set the size of memory in bytes + uint32_t get_memory_size(); // Return size of memory in bytes + void set_page_size(uint8_t page_size); // Set the size of the page we can write a page at a time + uint8_t get_page_size(); // Get the size of the page we can read a page at a time + void set_page_write_time(uint8_t write_time_ms); // Set the number of ms required per page write + uint8_t get_page_write_time(); // Get the number of ms required per page write + void set_i2c_buffer_size(uint8_t i2c_buffer_size); // Set the size of hw buffer -2 for control & addr + uint8_t get_i2c_buffer_size(); // Get the size of hw buffer -2 for control & addr + // Functionality to 'get' and 'put' objects to and from EEPROM. + template T &read_object(uint32_t idx, T &t) { + uint8_t *ptr = (uint8_t *) &t; + read(idx, ptr, sizeof(T)); // Address, data, sizeOfData + return t; + } + + template + T &write_object(uint32_t idx, T &t) // Address, data + { + uint8_t *ptr = (uint8_t *) &t; + write(idx, ptr, sizeof(T)); // Address, data, sizeOfData + return t; + } + + private: + void write_block_(uint32_t memaddr, const uint8_t *obj, uint8_t size); + uint32_t memory_size_bytes_{0}; + uint8_t memory_page_size_bytes_{0}; + uint8_t memory_page_write_time_ms_{0}; + bool poll_for_write_complete_{true}; + uint8_t i2c_buffer_size_{126}; +}; + +} // namespace external_eeprom +} // namespace esphome From 6b1f120dfe4710da1c23751d9002cf7376a58267 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 20 Jan 2024 17:03:06 +0000 Subject: [PATCH 02/20] Removed polling --- esphome/components/external_eeprom/external_eeprom.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 67ee3684f4..eb5a5b5fb4 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -151,7 +151,7 @@ uint32_t ExtEepromComponent::read_string_from_eeprom(uint32_t memaddr, std::stri /// @param memaddr is the location to write /// @param data_to_write contains the byte to be written void ExtEepromComponent::write8(uint32_t memaddr, uint8_t data_to_write) { - if (read8(memaddr) != data_to_write) { // Update only if data is new + if (read8(memaddr) != data_to_write) { // Update only if data is new write(memaddr, &data_to_write, 1); } } @@ -253,8 +253,7 @@ void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_ p += amt_to_write; size -= amt_to_write; ESP_LOGVV(TAG, "After write size %d amt %d add %d", size, amt_to_write, memaddr); - - delay(this->memory_page_write_time_ms_); /// Delay the amount of time to record a page + delay(this->memory_page_write_time_ms_); /// Delay the amount of time to record a page } } From ac9f52ef12e3b293d166885f71afee976c2fc6af Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 20 Jan 2024 17:13:16 +0000 Subject: [PATCH 03/20] fixed clang --- CODEOWNERS | 1 + esphome/components/external_eeprom/external_eeprom.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CODEOWNERS b/CODEOWNERS index 0ff5ce4508..346eec07f4 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -108,6 +108,7 @@ esphome/components/esp32_rmt_led_strip/* @jesserockz esphome/components/esp8266/* @esphome/core esphome/components/ethernet_info/* @gtjadsonsantos esphome/components/exposure_notifications/* @OttoWinter +esphome/components/external_eeprom/* @pebblebed-tech esphome/components/ezo/* @ssieb esphome/components/ezo_pmp/* @carlos-sarmiento esphome/components/factory_reset/* @anatoly-savchenkov diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index eb5a5b5fb4..dfe1ead522 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -254,7 +254,7 @@ void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_ size -= amt_to_write; ESP_LOGVV(TAG, "After write size %d amt %d add %d", size, amt_to_write, memaddr); delay(this->memory_page_write_time_ms_); /// Delay the amount of time to record a page - } + } } /// @brief Write a std::string to the device From cd06f54e1d565e309dc7ed13884e1acff025e99f Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Mon, 22 Jan 2024 18:14:30 +0000 Subject: [PATCH 04/20] Fixed redundant var --- esphome/components/external_eeprom/external_eeprom.cpp | 10 +++++----- esphome/components/external_eeprom/external_eeprom.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index dfe1ead522..4c8d56d885 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -113,16 +113,16 @@ void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_s i2c::ErrorCode ret; if (this->get_memory_size() > 2048) { uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; - ret = this->bus_->write(this->address_, maddr, 2, false); + ret = this->bus_->write(i2c_address, maddr, 2, false); } else { uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; - ret = this->bus_->write(this->address_, maddr, 1, false); + ret = this->bus_->write(i2c_address, maddr, 1, false); } if (ret != i2c::ERROR_OK) { ESP_LOGE(TAG, "Read raise this error %d on setting address", ret); } ESP_LOGVV(TAG, "Read - Done Set address, Ammount to read %d", amt_to_read); - ret = this->bus_->read(this->address_, p, amt_to_read); + ret = this->bus_->read(i2c_address, p, amt_to_read); if (ret != i2c::ERROR_OK) { ESP_LOGE(TAG, "Read raised this error %d on reading data", ret); } @@ -248,7 +248,7 @@ void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_ } ESP_LOGVV(TAG, "Write block %d bytes to address %d", amt_to_write, memaddr); - this->write_block_(memaddr, p, amt_to_write); + this->write_block_(i2c_address, memaddr, p, amt_to_write); memaddr += amt_to_write; p += amt_to_write; size -= amt_to_write; @@ -340,7 +340,7 @@ void ExtEepromComponent::set_i2c_buffer_size(uint8_t i2c_buffer_size) { i2c_buff uint8_t ExtEepromComponent::get_i2c_buffer_size() { return i2c_buffer_size_; } // private functions -void ExtEepromComponent::write_block_(uint32_t memaddr, const uint8_t *obj, uint8_t size) { +void ExtEepromComponent::write_block_(uint8_t deviceaddr, uint32_t memaddr, const uint8_t *obj, uint8_t size) { i2c::WriteBuffer buff[2]; i2c::ErrorCode ret; // Check if the device has two address bytes diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index 643a5fff04..c928366bf2 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -67,7 +67,7 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { } private: - void write_block_(uint32_t memaddr, const uint8_t *obj, uint8_t size); + void write_block_(uint8_t deviceaddr, uint32_t memaddr, const uint8_t *obj, uint8_t size); uint32_t memory_size_bytes_{0}; uint8_t memory_page_size_bytes_{0}; uint8_t memory_page_write_time_ms_{0}; From b96dbd371df23a49fb91c4bc908c6536c1a93328 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Mon, 22 Jan 2024 18:16:04 +0000 Subject: [PATCH 05/20] fixed redundant addr --- esphome/components/external_eeprom/external_eeprom.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index c928366bf2..acc6c85693 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -18,8 +18,7 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { float get_setup_priority() const override { return setup_priority::BUS; } bool is_connected(uint8_t i2c_address = 255); - bool is_busy(uint8_t i2c_address = 255); - + uint8_t read8(uint32_t memaddr); // Read a single byte from address memaddr uint16_t read16(uint32_t memaddr); uint32_t read32(uint32_t memaddr); From cd67f91fad12bdbc3036c1d018e01d6b1bfeca9d Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Mon, 22 Jan 2024 18:21:23 +0000 Subject: [PATCH 06/20] Yaml Tests re-added --- tests/test1.yaml | 9 +++++++++ tests/test3.1.yaml | 8 ++++++++ tests/test5.yaml | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/tests/test1.yaml b/tests/test1.yaml index 3ca6faca8a..5e6cd96105 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -4330,3 +4330,12 @@ alarm_control_panel: then: - lambda: !lambda |- ESP_LOGD("TEST", "State change %s", alarm_control_panel_state_to_string(id(alarmcontrolpanel1)->get_state())); + +external_eeprom: + id: ext_eeprom_component_1 + address: 0x57 + ee_page_write_time: 5 + ee_page_size: 32 + ee_memory_size: 4096 + i2c_buffer_size: 126 + i2c_id: i2c_bus diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index b5428abbfa..d4e2cedce1 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -732,3 +732,11 @@ adc128s102: cs_pin: allow_other_uses: true number: GPIO12 + +external_eeprom: + id: ext_eeprom_component_1 + address: 0x57 + ee_page_write_time: 5 + ee_page_size: 32 + ee_memory_size: 4096 + i2c_buffer_size: 126 diff --git a/tests/test5.yaml b/tests/test5.yaml index bf4247fb92..548013430e 100644 --- a/tests/test5.yaml +++ b/tests/test5.yaml @@ -730,3 +730,11 @@ light: bit0_low: 100us bit1_high: 100us bit1_low: 100us + +external_eeprom: + id: ext_eeprom_component_1 + address: 0x57 + ee_page_write_time: 5 + ee_page_size: 32 + ee_memory_size: 4096 + i2c_buffer_size: 126 From f43da4cd327727474b2e193093ae0f5bf3474c5a Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Mon, 22 Jan 2024 18:24:31 +0000 Subject: [PATCH 07/20] more clang --- esphome/components/external_eeprom/external_eeprom.h | 1 - 1 file changed, 1 deletion(-) diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index acc6c85693..2927428b49 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -18,7 +18,6 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { float get_setup_priority() const override { return setup_priority::BUS; } bool is_connected(uint8_t i2c_address = 255); - uint8_t read8(uint32_t memaddr); // Read a single byte from address memaddr uint16_t read16(uint32_t memaddr); uint32_t read32(uint32_t memaddr); From 5b69a6d6334104108f49e874f95b8729f25ff64a Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Mon, 22 Jan 2024 18:43:14 +0000 Subject: [PATCH 08/20] clang unsed method removal --- esphome/components/external_eeprom/external_eeprom.h | 1 - 1 file changed, 1 deletion(-) diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index 2927428b49..772bb31d7b 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -69,7 +69,6 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { uint32_t memory_size_bytes_{0}; uint8_t memory_page_size_bytes_{0}; uint8_t memory_page_write_time_ms_{0}; - bool poll_for_write_complete_{true}; uint8_t i2c_buffer_size_{126}; }; From a3e3ac019271886d5bd75e37ab979974a7afe46c Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 10 Feb 2024 10:23:46 +0000 Subject: [PATCH 09/20] Simplified config options --- .../components/external_eeprom/__init__.py | 28 ++- .../external_eeprom/external_eeprom.cpp | 161 +++++++++++++----- .../external_eeprom/external_eeprom.h | 40 ++++- 3 files changed, 173 insertions(+), 56 deletions(-) diff --git a/esphome/components/external_eeprom/__init__.py b/esphome/components/external_eeprom/__init__.py index ad764debe9..08dfe2dd90 100644 --- a/esphome/components/external_eeprom/__init__.py +++ b/esphome/components/external_eeprom/__init__.py @@ -7,9 +7,7 @@ CODEOWNERS = ["@pebblebed-tech"] DEPENDENCIES = ["i2c"] MULTI_CONF = True -CONF_EE_MEMORY_SIZE = "ee_memory_size" -CONF_EE_PAGE_SIZE = "ee_page_size" -CONF_EE_PAGE_WRITE_TIME = "ee_page_write_time" +CONF_EE_MEMORY_TYPE = "ee_memory_type" CONF_I2C_BUFFER_SIZE = "i2c_buffer_size" ext_eeprom_component_ns = cg.esphome_ns.namespace("external_eeprom") @@ -17,13 +15,27 @@ ExtEepromComponent = ext_eeprom_component_ns.class_( "ExtEepromComponent", cg.Component, i2c.I2CDevice ) +EEPROM_TYPES = { + "24XX00": ext_eeprom_component_ns.EEE_24XX00, + "24XX01": ext_eeprom_component_ns.EEE_24XX01, + "24XX02": ext_eeprom_component_ns.EEE_24XX02, + "24XX04": ext_eeprom_component_ns.EEE_24XX04, + "24XX08": ext_eeprom_component_ns.EEE_24XX08, + "24XX16": ext_eeprom_component_ns.EEE_24XX16, + "24XX32": ext_eeprom_component_ns.EEE_24XX32, + "24XX64": ext_eeprom_component_ns.EEE_24XX64, + "24XX128": ext_eeprom_component_ns.EEE_24XX128, + "24XX256": ext_eeprom_component_ns.EEE_24XX256, + "24XX512": ext_eeprom_component_ns.EEE_24XX512, + "24XX1025": ext_eeprom_component_ns.EEE_24XX1025, + "24XX2048": ext_eeprom_component_ns.EEE_24XX2048, +} + CONFIG_SCHEMA = ( cv.Schema( { cv.GenerateID(): cv.declare_id(ExtEepromComponent), - cv.Required(CONF_EE_MEMORY_SIZE): cv.uint32_t, - cv.Required(CONF_EE_PAGE_SIZE): cv.uint16_t, - cv.Required(CONF_EE_PAGE_WRITE_TIME): cv.uint8_t, + cv.Required(CONF_EE_MEMORY_TYPE): cv.one_of(*EEPROM_TYPES, upper=True), cv.Required(CONF_I2C_BUFFER_SIZE): cv.uint8_t, } ) @@ -36,7 +48,5 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await i2c.register_i2c_device(var, config) - cg.add(var.set_memory_size(config[CONF_EE_MEMORY_SIZE])) - cg.add(var.set_page_size(config[CONF_EE_PAGE_SIZE])) - cg.add(var.set_page_write_time(config[CONF_EE_PAGE_WRITE_TIME])) cg.add(var.set_i2c_buffer_size(config[CONF_I2C_BUFFER_SIZE])) + cg.add(var.set_memory_type(EEPROM_TYPES[config[CONF_EE_MEMORY_TYPE]])) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 4c8d56d885..36a72b87f6 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -19,13 +19,12 @@ void ExtEepromComponent::loop() {} void ExtEepromComponent::dump_config() { ESP_LOGCONFIG(TAG, "External Eeprom"); LOG_I2C_DEVICE(this); - ESP_LOGCONFIG(TAG, "Size = %d", this->get_memory_size()); - ESP_LOGCONFIG(TAG, "Page size = %d", this->get_page_size()); + ESP_LOGCONFIG(TAG, "Device Type = %s", this->device_type_text_.c_str()); + ESP_LOGCONFIG(TAG, "Size = %d", this->get_memory_size_()); + ESP_LOGCONFIG(TAG, "Page size = %d", this->get_page_size_()); + ESP_LOGCONFIG(TAG, "Number of Address Bytes = %d", this->get_address_size_bytes_()); ESP_LOGCONFIG(TAG, "I2C HW buffer size = %d", this->get_i2c_buffer_size()); - ESP_LOGCONFIG(TAG, "Page write time = %d", this->get_page_write_time()); - if (this->is_failed()) { - ESP_LOGE(TAG, "Communication with device failed!"); - } + ESP_LOGCONFIG(TAG, "Page write time = %d", this->get_page_write_time_()); } /// @brief This checks whether the device is connected and not busy /// @param Caller can pass in an 0xFF I2C address. This is helpful for larger EEPROMs that have two addresses (see block @@ -33,7 +32,7 @@ void ExtEepromComponent::dump_config() { /// @return an boolean True for connected bool ExtEepromComponent::is_connected(uint8_t i2c_address) { i2c::ErrorCode err; - if (i2c_address == 255) + if (i2c_address == 255) // We can't set the default so we use 255 instead i2c_address = this->address_; err = this->bus_->write(i2c_address, nullptr, 0, true); if (err != i2c::ERROR_OK) @@ -91,15 +90,16 @@ void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_s ESP_LOGVV(TAG, "Read %d bytes from address %d", buffer_size, memaddr); uint32_t size = buffer_size; uint8_t *p = buff; + i2c::ErrorCode ret; while (size >= 1) { // Limit the amount to read to a page size uint16_t amt_to_read = size; - if (amt_to_read > i2c_buffer_size_) // I2C buffer size limit - amt_to_read = i2c_buffer_size_; + if (amt_to_read > this->i2c_buffer_size_) // I2C buffer size limit + amt_to_read = this->i2c_buffer_size_; // Check if we are dealing with large (>512kbit) EEPROMs uint8_t i2c_address = this->address_; - if (this->get_memory_size() > 0xFFFF) { + if (this->get_memory_size_() > 0xFFFF) { // Figure out if we are going to cross the barrier with this read if (memaddr < 0xFFFF) { if (0xFFFF - memaddr < amt_to_read) // 0xFFFF - 0xFFFA < I2C_buffer_size @@ -110,11 +110,13 @@ void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_s if (memaddr > 0xFFFF) i2c_address |= 0b100; // Set the block bit to 1 } - i2c::ErrorCode ret; - if (this->get_memory_size() > 2048) { + if (this->get_address_size_bytes_() == 2) + { uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; ret = this->bus_->write(i2c_address, maddr, 2, false); - } else { + } + else + { uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; ret = this->bus_->write(i2c_address, maddr, 1, false); } @@ -213,8 +215,8 @@ void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_ } uint16_t max_write_size = this->memory_page_size_bytes_; - if (max_write_size > i2c_buffer_size_) - max_write_size = i2c_buffer_size_; + if (max_write_size > this->i2c_buffer_size_) + max_write_size = this->i2c_buffer_size_; /// Break the buffer into page sized chunks @@ -235,7 +237,7 @@ void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_ } /// Check if we are dealing with large (>512kbit) EEPROMs uint8_t i2c_address = this->address_; - if (this->get_memory_size() > 0xFFFF) { + if (this->get_memory_size_() > 0xFFFF) { /// Figure out if we are going to cross the barrier with this write if (memaddr < 0xFFFF) { if (0xFFFF - memaddr < amt_to_write) /// 0xFFFF - 0xFFFA < I2C_buffer_size @@ -311,30 +313,82 @@ void ExtEepromComponent::erase(uint8_t value_to_write) { for (uint32_t x = 0; x < this->memory_page_size_bytes_; x++) temp_buffer[x] = value_to_write; - for (uint32_t addr = 0; addr < this->get_memory_size(); addr += this->memory_page_size_bytes_) + for (uint32_t addr = 0; addr < this->get_memory_size_(); addr += this->memory_page_size_bytes_) write(addr, temp_buffer, this->memory_page_size_bytes_); } -/// @brief Sets the size of the device in bytes -/// @param memSize contains the size of the device -void ExtEepromComponent::set_memory_size(uint32_t mem_size) { memory_size_bytes_ = mem_size; } -/// @brief Gets the user specified size of the device in bytes -/// @return size in bytes -uint32_t ExtEepromComponent::get_memory_size() { return memory_size_bytes_; } -/// @brief Sets the page size of the device in bytes -/// @param page_size contains the size of the device pages -void ExtEepromComponent::set_page_size(uint8_t page_size) { memory_page_size_bytes_ = page_size; } -/// @brief Gets the user specified size of the device pages in bytes -/// @return Page size in bytes -uint8_t ExtEepromComponent::get_page_size() { return memory_page_size_bytes_; } -/// @brief Sets the page write for the device in ms -/// @param write_time_ms contains the time to write a page of the device -void ExtEepromComponent::set_page_write_time(uint8_t write_time_ms) { memory_page_write_time_ms_ = write_time_ms; } -/// @brief Gets the user specified write time for a device page in ms -/// @return page write time in ms -uint8_t ExtEepromComponent::get_page_write_time() { return memory_page_write_time_ms_; } +void ExtEepromComponent::set_memory_type(EEEDeviceType device_type) { + device_type_ = device_type; + // Set settings based on known memory types + switch (device_type_) + { + default: + // Unknown type number + break; + case EEE_24XX00: + this->device_type_text_ = "24XX00"; + this->set_device_config_(16, 1, 1, 5); + break; + case EEE_24XX01: + this->device_type_text_ = "24XX01"; + this->set_device_config_(128, 1, 8, 5); // 128 + break; + case EEE_24XX02: + this->device_type_text_ = "24XX02"; + this->set_device_config_(256, 1, 8, 5); // 256 + break; + case EEE_24XX04: + this->device_type_text_ = "24XX04"; + this->set_device_config_(512, 1, 16, 5); // 512 + break; + case EEE_24XX08: + this->device_type_text_ = "24XX08"; + this->set_device_config_(1024, 1, 16, 5); // 1024 + break; + case EEE_24XX16: + this->device_type_text_ = "24XX16"; + this->set_device_config_(2048, 1, 16, 1); // 2048 + break; + case EEE_24XX32: + this->device_type_text_ = "24XX32"; + this->set_device_config_(4096, 2, 32, 5); // 4096 + break; + case EEE_24XX64: + this->device_type_text_ = "24XX64"; + this->set_device_config_(8192, 2, 32, 5); // 8192 + break; + case EEE_24XX128: + this->device_type_text_ = "24XX128"; + this->set_device_config_(16384, 2, 64, 5); // 16384 + break; + case EEE_24XX256: + this->device_type_text_ = "24XX256"; + this->set_device_config_(32768, 2, 64, 5); // 32768 + break; + case EEE_24XX512: + this->device_type_text_ = "24XX512"; + this->set_device_config_(65536, 2, 64, 5); // 65536 + break; + case EEE_24XX1025: + this->device_type_text_ = "24XX1025"; + this->set_device_config_(128000, 2, 128, 5); // 128000 + break; + case EEE_24XX2048: + this->device_type_text_ = "24XX2048"; + this->set_device_config_(262144, 2, 256, 5); // 262144 + break; + } +} +void ExtEepromComponent::set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, uint8_t write_time_ms) +{ + this->set_memory_size_(mem_size); + this->set_address_size_bytes_(address_bytes); + this->set_page_size_(page_size); + this->set_page_write_time_(write_time_ms); +} + /// @brief Sets the hw I2C buffer size -2, as 2 bytes are needed for control & addr /// @param buffer size in bytes, (ESP devices has a 128 I2C buffer so it is set to 126) -void ExtEepromComponent::set_i2c_buffer_size(uint8_t i2c_buffer_size) { i2c_buffer_size_ = i2c_buffer_size; } +void ExtEepromComponent::set_i2c_buffer_size(uint8_t i2c_buffer_size) { i2c_buffer_size_ = i2c_buffer_size - 2; } /// @brief Gets the hw I2C buffer size -2, as 2 bytes are needed for control & addr /// @return buffer size in bytes uint8_t ExtEepromComponent::get_i2c_buffer_size() { return i2c_buffer_size_; } @@ -344,14 +398,17 @@ void ExtEepromComponent::write_block_(uint8_t deviceaddr, uint32_t memaddr, cons i2c::WriteBuffer buff[2]; i2c::ErrorCode ret; // Check if the device has two address bytes - if (this->get_memory_size() > 2048) { + if (this->get_address_size_bytes_() == 2) + { uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; buff[0].data = maddr; buff[0].len = 2; buff[1].data = obj; buff[1].len = size; ret = this->bus_->writev(this->address_, buff, 2, true); - } else { + } + else + { uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; buff[0].data = maddr; buff[0].len = 1; @@ -363,6 +420,32 @@ void ExtEepromComponent::write_block_(uint8_t deviceaddr, uint32_t memaddr, cons ESP_LOGE(TAG, "Write raise this error %d on writing data to this address %d", ret, memaddr); } } - +// @brief Sets the size of the device in bytes +/// @param memSize contains the size of the device +void ExtEepromComponent::set_memory_size_(uint32_t mem_size) { memory_size_bytes_ = mem_size; } +/// @brief Gets the user specified size of the device in bytes +/// @return size in bytes +uint32_t ExtEepromComponent::get_memory_size_() { return memory_size_bytes_; } +/// @brief Sets the page size of the device in bytes +/// @param page_size contains the size of the device pages +void ExtEepromComponent::set_page_size_(uint16_t page_size) { memory_page_size_bytes_ = page_size; } +/// @brief Gets the user specified size of the device pages in bytes +/// @return Page size in bytes +uint16_t ExtEepromComponent::get_page_size_() { return memory_page_size_bytes_; } +/// @brief Sets the page write for the device in ms +/// @param write_time_ms contains the time to write a page of the device +void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { memory_page_write_time_ms_ = write_time_ms; } +/// @brief Gets the user specified write time for a device page in ms +/// @return page write time in ms +uint8_t ExtEepromComponent::get_page_write_time_() { return memory_page_write_time_ms_; } +/// @brief Set address_bytes for the device +/// @param address_bytes contains the number of bytes the device uses for address +void ExtEepromComponent::set_address_size_bytes_(uint8_t address_bytes) +{ + this->address_size_bytes_ = address_bytes; +} +/// @brief Gets the number of bytes used for the address +/// @return size in bytes +uint8_t ExtEepromComponent::get_address_size_bytes_() { return this->address_size_bytes_; } } // namespace external_eeprom } // namespace esphome diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index 772bb31d7b..d29f9b3c7c 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -9,7 +9,22 @@ namespace external_eeprom { /// @brief This Class provides the methods to read and write data from an 24 LC/AT XX devices such as 24LC32. See /// https://ww1.microchip.com/downloads/en/devicedoc/doc0336.pdf - +enum EEEDeviceType +{ + EEE_24XX00, + EEE_24XX01, + EEE_24XX02, + EEE_24XX04, + EEE_24XX08, + EEE_24XX16, + EEE_24XX32, + EEE_24XX64, + EEE_24XX128, + EEE_24XX256, + EEE_24XX512, + EEE_24XX1025, + EEE_24XX2048 +}; class ExtEepromComponent : public i2c::I2CDevice, public Component { public: void setup() override; @@ -41,12 +56,9 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { void erase(uint8_t value_to_write = 0x00); // Erase the entire memory. Optional: write a given byte to each spot. // Getters and Setters for component config - void set_memory_size(uint32_t mem_size); // Set the size of memory in bytes - uint32_t get_memory_size(); // Return size of memory in bytes - void set_page_size(uint8_t page_size); // Set the size of the page we can write a page at a time - uint8_t get_page_size(); // Get the size of the page we can read a page at a time - void set_page_write_time(uint8_t write_time_ms); // Set the number of ms required per page write - uint8_t get_page_write_time(); // Get the number of ms required per page write + void set_memory_type(EEEDeviceType device_type); + + void set_i2c_buffer_size(uint8_t i2c_buffer_size); // Set the size of hw buffer -2 for control & addr uint8_t get_i2c_buffer_size(); // Get the size of hw buffer -2 for control & addr // Functionality to 'get' and 'put' objects to and from EEPROM. @@ -66,10 +78,22 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { private: void write_block_(uint8_t deviceaddr, uint32_t memaddr, const uint8_t *obj, uint8_t size); + void set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, uint8_t write_time_ms); + void set_memory_size_(uint32_t mem_size); // Set the size of memory in bytes + uint32_t get_memory_size_(); // Return size of memory in bytes + void set_page_size_(uint16_t page_size); // Set the size of the page we can write a page at a time + uint16_t get_page_size_(); // Get the size of the page we can read a page at a time + void set_address_size_bytes_(uint8_t address_size_bytes); // Set the number of bytes to use for device address + uint8_t get_address_size_bytes_(); // Get the number of bytes to use for device address + void set_page_write_time_(uint8_t write_time_ms); // Set the number of ms required per page write + uint8_t get_page_write_time_(); // Get the number of ms required per page write uint32_t memory_size_bytes_{0}; - uint8_t memory_page_size_bytes_{0}; + uint16_t memory_page_size_bytes_{0}; + uint8_t address_size_bytes_{0}; uint8_t memory_page_write_time_ms_{0}; uint8_t i2c_buffer_size_{126}; + EEEDeviceType device_type_{EEE_24XX32}; + std::string device_type_text_{""}; }; } // namespace external_eeprom From fcb7362d96ba18bdba5ad89116a27d4096132284 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 10 Feb 2024 10:43:29 +0000 Subject: [PATCH 10/20] Fix clang and update tests --- .../external_eeprom/external_eeprom.cpp | 141 ++++++++---------- .../external_eeprom/external_eeprom.h | 19 +-- tests/test1.yaml | 6 +- tests/test3.1.yaml | 6 +- tests/test5.yaml | 6 +- 5 files changed, 80 insertions(+), 98 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 36a72b87f6..10cae80a55 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -32,7 +32,7 @@ void ExtEepromComponent::dump_config() { /// @return an boolean True for connected bool ExtEepromComponent::is_connected(uint8_t i2c_address) { i2c::ErrorCode err; - if (i2c_address == 255) // We can't set the default so we use 255 instead + if (i2c_address == 255) // We can't set the default so we use 255 instead i2c_address = this->address_; err = this->bus_->write(i2c_address, nullptr, 0, true); if (err != i2c::ERROR_OK) @@ -110,13 +110,10 @@ void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_s if (memaddr > 0xFFFF) i2c_address |= 0b100; // Set the block bit to 1 } - if (this->get_address_size_bytes_() == 2) - { + if (this->get_address_size_bytes_() == 2) { uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; ret = this->bus_->write(i2c_address, maddr, 2, false); - } - else - { + } else { uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; ret = this->bus_->write(i2c_address, maddr, 1, false); } @@ -318,72 +315,72 @@ void ExtEepromComponent::erase(uint8_t value_to_write) { } void ExtEepromComponent::set_memory_type(EEEDeviceType device_type) { device_type_ = device_type; - // Set settings based on known memory types - switch (device_type_) - { - default: - // Unknown type number - break; - case EEE_24XX00: - this->device_type_text_ = "24XX00"; - this->set_device_config_(16, 1, 1, 5); - break; - case EEE_24XX01: - this->device_type_text_ = "24XX01"; - this->set_device_config_(128, 1, 8, 5); // 128 - break; - case EEE_24XX02: - this->device_type_text_ = "24XX02"; - this->set_device_config_(256, 1, 8, 5); // 256 - break; - case EEE_24XX04: - this->device_type_text_ = "24XX04"; - this->set_device_config_(512, 1, 16, 5); // 512 - break; - case EEE_24XX08: - this->device_type_text_ = "24XX08"; - this->set_device_config_(1024, 1, 16, 5); // 1024 - break; - case EEE_24XX16: - this->device_type_text_ = "24XX16"; - this->set_device_config_(2048, 1, 16, 1); // 2048 - break; - case EEE_24XX32: - this->device_type_text_ = "24XX32"; - this->set_device_config_(4096, 2, 32, 5); // 4096 - break; - case EEE_24XX64: - this->device_type_text_ = "24XX64"; - this->set_device_config_(8192, 2, 32, 5); // 8192 - break; - case EEE_24XX128: - this->device_type_text_ = "24XX128"; - this->set_device_config_(16384, 2, 64, 5); // 16384 - break; - case EEE_24XX256: - this->device_type_text_ = "24XX256"; - this->set_device_config_(32768, 2, 64, 5); // 32768 - break; - case EEE_24XX512: - this->device_type_text_ = "24XX512"; - this->set_device_config_(65536, 2, 64, 5); // 65536 - break; - case EEE_24XX1025: - this->device_type_text_ = "24XX1025"; - this->set_device_config_(128000, 2, 128, 5); // 128000 - break; - case EEE_24XX2048: - this->device_type_text_ = "24XX2048"; - this->set_device_config_(262144, 2, 256, 5); // 262144 - break; + // Set settings based on known memory types + switch (device_type_) { + default: + // Unknown type number + break; + case EEE_24XX00: + this->device_type_text_ = "24XX00"; + this->set_device_config_(16, 1, 1, 5); + break; + case EEE_24XX01: + this->device_type_text_ = "24XX01"; + this->set_device_config_(128, 1, 8, 5); // 128 + break; + case EEE_24XX02: + this->device_type_text_ = "24XX02"; + this->set_device_config_(256, 1, 8, 5); // 256 + break; + case EEE_24XX04: + this->device_type_text_ = "24XX04"; + this->set_device_config_(512, 1, 16, 5); // 512 + break; + case EEE_24XX08: + this->device_type_text_ = "24XX08"; + this->set_device_config_(1024, 1, 16, 5); // 1024 + break; + case EEE_24XX16: + this->device_type_text_ = "24XX16"; + this->set_device_config_(2048, 1, 16, 1); // 2048 + break; + case EEE_24XX32: + this->device_type_text_ = "24XX32"; + this->set_device_config_(4096, 2, 32, 5); // 4096 + break; + case EEE_24XX64: + this->device_type_text_ = "24XX64"; + this->set_device_config_(8192, 2, 32, 5); // 8192 + break; + case EEE_24XX128: + this->device_type_text_ = "24XX128"; + this->set_device_config_(16384, 2, 64, 5); // 16384 + break; + case EEE_24XX256: + this->device_type_text_ = "24XX256"; + this->set_device_config_(32768, 2, 64, 5); // 32768 + break; + case EEE_24XX512: + this->device_type_text_ = "24XX512"; + this->set_device_config_(65536, 2, 64, 5); // 65536 + break; + case EEE_24XX1025: + this->device_type_text_ = "24XX1025"; + this->set_device_config_(128000, 2, 128, 5); // 128000 + break; + case EEE_24XX2048: + this->device_type_text_ = "24XX2048"; + this->set_device_config_(262144, 2, 256, 5); // 262144 + break; } } -void ExtEepromComponent::set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, uint8_t write_time_ms) +void ExtEepromComponent::set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, + uint8_t write_time_ms) { this->set_memory_size_(mem_size); this->set_address_size_bytes_(address_bytes); this->set_page_size_(page_size); - this->set_page_write_time_(write_time_ms); + this->set_page_write_time_(write_time_ms); } /// @brief Sets the hw I2C buffer size -2, as 2 bytes are needed for control & addr @@ -398,17 +395,14 @@ void ExtEepromComponent::write_block_(uint8_t deviceaddr, uint32_t memaddr, cons i2c::WriteBuffer buff[2]; i2c::ErrorCode ret; // Check if the device has two address bytes - if (this->get_address_size_bytes_() == 2) - { + if (this->get_address_size_bytes_() == 2) { uint8_t maddr[] = {(uint8_t) (memaddr >> 8), (uint8_t) (memaddr & 0xFF)}; buff[0].data = maddr; buff[0].len = 2; buff[1].data = obj; buff[1].len = size; ret = this->bus_->writev(this->address_, buff, 2, true); - } - else - { + } else { uint8_t maddr[] = {(uint8_t) (memaddr & 0xFF)}; buff[0].data = maddr; buff[0].len = 1; @@ -440,10 +434,7 @@ void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { memory_pa uint8_t ExtEepromComponent::get_page_write_time_() { return memory_page_write_time_ms_; } /// @brief Set address_bytes for the device /// @param address_bytes contains the number of bytes the device uses for address -void ExtEepromComponent::set_address_size_bytes_(uint8_t address_bytes) -{ - this->address_size_bytes_ = address_bytes; -} +void ExtEepromComponent::set_address_size_bytes_(uint8_t address_bytes) { this->address_size_bytes_ = address_bytes; } /// @brief Gets the number of bytes used for the address /// @return size in bytes uint8_t ExtEepromComponent::get_address_size_bytes_() { return this->address_size_bytes_; } diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index d29f9b3c7c..39d2fda962 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -9,8 +9,7 @@ namespace external_eeprom { /// @brief This Class provides the methods to read and write data from an 24 LC/AT XX devices such as 24LC32. See /// https://ww1.microchip.com/downloads/en/devicedoc/doc0336.pdf -enum EEEDeviceType -{ +enum EEEDeviceType { EEE_24XX00, EEE_24XX01, EEE_24XX02, @@ -57,8 +56,6 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { // Getters and Setters for component config void set_memory_type(EEEDeviceType device_type); - - void set_i2c_buffer_size(uint8_t i2c_buffer_size); // Set the size of hw buffer -2 for control & addr uint8_t get_i2c_buffer_size(); // Get the size of hw buffer -2 for control & addr // Functionality to 'get' and 'put' objects to and from EEPROM. @@ -79,14 +76,14 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { private: void write_block_(uint8_t deviceaddr, uint32_t memaddr, const uint8_t *obj, uint8_t size); void set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, uint8_t write_time_ms); - void set_memory_size_(uint32_t mem_size); // Set the size of memory in bytes - uint32_t get_memory_size_(); // Return size of memory in bytes - void set_page_size_(uint16_t page_size); // Set the size of the page we can write a page at a time - uint16_t get_page_size_(); // Get the size of the page we can read a page at a time + void set_memory_size_(uint32_t mem_size); // Set the size of memory in bytes + uint32_t get_memory_size_(); // Return size of memory in bytes + void set_page_size_(uint16_t page_size); // Set the size of the page we can write a page at a time + uint16_t get_page_size_(); // Get the size of the page we can read a page at a time void set_address_size_bytes_(uint8_t address_size_bytes); // Set the number of bytes to use for device address - uint8_t get_address_size_bytes_(); // Get the number of bytes to use for device address - void set_page_write_time_(uint8_t write_time_ms); // Set the number of ms required per page write - uint8_t get_page_write_time_(); // Get the number of ms required per page write + uint8_t get_address_size_bytes_(); // Get the number of bytes to use for device address + void set_page_write_time_(uint8_t write_time_ms); // Set the number of ms required per page write + uint8_t get_page_write_time_(); // Get the number of ms required per page write uint32_t memory_size_bytes_{0}; uint16_t memory_page_size_bytes_{0}; uint8_t address_size_bytes_{0}; diff --git a/tests/test1.yaml b/tests/test1.yaml index 5e6cd96105..b1b2b31207 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -4334,8 +4334,6 @@ alarm_control_panel: external_eeprom: id: ext_eeprom_component_1 address: 0x57 - ee_page_write_time: 5 - ee_page_size: 32 - ee_memory_size: 4096 - i2c_buffer_size: 126 + ee_memory_type: 24XX32 + i2c_buffer_size: 128 i2c_id: i2c_bus diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index d4e2cedce1..9399f9ad83 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -736,7 +736,5 @@ adc128s102: external_eeprom: id: ext_eeprom_component_1 address: 0x57 - ee_page_write_time: 5 - ee_page_size: 32 - ee_memory_size: 4096 - i2c_buffer_size: 126 + ee_memory_type: 24XX32 + i2c_buffer_size: 128 diff --git a/tests/test5.yaml b/tests/test5.yaml index 548013430e..314545098a 100644 --- a/tests/test5.yaml +++ b/tests/test5.yaml @@ -734,7 +734,5 @@ light: external_eeprom: id: ext_eeprom_component_1 address: 0x57 - ee_page_write_time: 5 - ee_page_size: 32 - ee_memory_size: 4096 - i2c_buffer_size: 126 + ee_memory_type: 24XX32 + i2c_buffer_size: 128 From 99d35fa6adc5a0f52ff2251140e38a7b1c7c3e49 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 10 Feb 2024 11:04:15 +0000 Subject: [PATCH 11/20] more clang --- .../external_eeprom/external_eeprom.cpp | 27 +++++++++---------- .../external_eeprom/external_eeprom.h | 16 +++++------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 10cae80a55..39a0f06479 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -326,57 +326,56 @@ void ExtEepromComponent::set_memory_type(EEEDeviceType device_type) { break; case EEE_24XX01: this->device_type_text_ = "24XX01"; - this->set_device_config_(128, 1, 8, 5); // 128 + this->set_device_config_(128, 1, 8, 5); // 128 break; case EEE_24XX02: this->device_type_text_ = "24XX02"; - this->set_device_config_(256, 1, 8, 5); // 256 + this->set_device_config_(256, 1, 8, 5); // 256 break; case EEE_24XX04: this->device_type_text_ = "24XX04"; - this->set_device_config_(512, 1, 16, 5); // 512 + this->set_device_config_(512, 1, 16, 5); // 512 break; case EEE_24XX08: this->device_type_text_ = "24XX08"; - this->set_device_config_(1024, 1, 16, 5); // 1024 + this->set_device_config_(1024, 1, 16, 5); // 1024 break; case EEE_24XX16: this->device_type_text_ = "24XX16"; - this->set_device_config_(2048, 1, 16, 1); // 2048 + this->set_device_config_(2048, 1, 16, 1); // 2048 break; case EEE_24XX32: this->device_type_text_ = "24XX32"; - this->set_device_config_(4096, 2, 32, 5); // 4096 + this->set_device_config_(4096, 2, 32, 5); // 4096 break; case EEE_24XX64: this->device_type_text_ = "24XX64"; - this->set_device_config_(8192, 2, 32, 5); // 8192 + this->set_device_config_(8192, 2, 32, 5); // 8192 break; case EEE_24XX128: this->device_type_text_ = "24XX128"; - this->set_device_config_(16384, 2, 64, 5); // 16384 + this->set_device_config_(16384, 2, 64, 5); // 16384 break; case EEE_24XX256: this->device_type_text_ = "24XX256"; - this->set_device_config_(32768, 2, 64, 5); // 32768 + this->set_device_config_(32768, 2, 64, 5); // 32768 break; case EEE_24XX512: this->device_type_text_ = "24XX512"; - this->set_device_config_(65536, 2, 64, 5); // 65536 + this->set_device_config_(65536, 2, 64, 5); // 65536 break; case EEE_24XX1025: this->device_type_text_ = "24XX1025"; - this->set_device_config_(128000, 2, 128, 5); // 128000 + this->set_device_config_(128000, 2, 128, 5); // 128000 break; case EEE_24XX2048: this->device_type_text_ = "24XX2048"; - this->set_device_config_(262144, 2, 256, 5); // 262144 + this->set_device_config_(262144, 2, 256, 5); // 262144 break; } } void ExtEepromComponent::set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, - uint8_t write_time_ms) -{ + uint8_t write_time_ms) { this->set_memory_size_(mem_size); this->set_address_size_bytes_(address_bytes); this->set_page_size_(page_size); diff --git a/esphome/components/external_eeprom/external_eeprom.h b/esphome/components/external_eeprom/external_eeprom.h index 39d2fda962..900e66516f 100644 --- a/esphome/components/external_eeprom/external_eeprom.h +++ b/esphome/components/external_eeprom/external_eeprom.h @@ -76,14 +76,14 @@ class ExtEepromComponent : public i2c::I2CDevice, public Component { private: void write_block_(uint8_t deviceaddr, uint32_t memaddr, const uint8_t *obj, uint8_t size); void set_device_config_(uint32_t mem_size, uint8_t address_bytes, uint16_t page_size, uint8_t write_time_ms); - void set_memory_size_(uint32_t mem_size); // Set the size of memory in bytes - uint32_t get_memory_size_(); // Return size of memory in bytes - void set_page_size_(uint16_t page_size); // Set the size of the page we can write a page at a time - uint16_t get_page_size_(); // Get the size of the page we can read a page at a time - void set_address_size_bytes_(uint8_t address_size_bytes); // Set the number of bytes to use for device address - uint8_t get_address_size_bytes_(); // Get the number of bytes to use for device address - void set_page_write_time_(uint8_t write_time_ms); // Set the number of ms required per page write - uint8_t get_page_write_time_(); // Get the number of ms required per page write + void set_memory_size_(uint32_t mem_size); // Set the size of memory in bytes + uint32_t get_memory_size_(); // Return size of memory in bytes + void set_page_size_(uint16_t page_size); // Set the size of the page we can write a page at a time + uint16_t get_page_size_(); // Get the size of the page we can read a page at a time + void set_address_size_bytes_(uint8_t address_size_bytes); // Set the number of bytes to use for device address + uint8_t get_address_size_bytes_(); // Get the number of bytes to use for device address + void set_page_write_time_(uint8_t write_time_ms); // Set the number of ms required per page write + uint8_t get_page_write_time_(); // Get the number of ms required per page write uint32_t memory_size_bytes_{0}; uint16_t memory_page_size_bytes_{0}; uint8_t address_size_bytes_{0}; From 6d8b54ed42bbde1250b2fe13e453465a9d6fac26 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 10 Feb 2024 11:41:37 +0000 Subject: [PATCH 12/20] fixed var name --- esphome/components/external_eeprom/external_eeprom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 39a0f06479..c42a87f1c5 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -433,7 +433,7 @@ void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { memory_pa uint8_t ExtEepromComponent::get_page_write_time_() { return memory_page_write_time_ms_; } /// @brief Set address_bytes for the device /// @param address_bytes contains the number of bytes the device uses for address -void ExtEepromComponent::set_address_size_bytes_(uint8_t address_bytes) { this->address_size_bytes_ = address_bytes; } +void ExtEepromComponent::set_address_size_bytes_(uint8_t address_size_bytes) { this->address_size_bytes_ = address_size_bytes; } /// @brief Gets the number of bytes used for the address /// @return size in bytes uint8_t ExtEepromComponent::get_address_size_bytes_() { return this->address_size_bytes_; } From 965efa63afe532c23468b29515f980b479eef861 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 10 Feb 2024 11:49:05 +0000 Subject: [PATCH 13/20] more clang --- esphome/components/external_eeprom/external_eeprom.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index c42a87f1c5..43b4e431c0 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -433,7 +433,9 @@ void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { memory_pa uint8_t ExtEepromComponent::get_page_write_time_() { return memory_page_write_time_ms_; } /// @brief Set address_bytes for the device /// @param address_bytes contains the number of bytes the device uses for address -void ExtEepromComponent::set_address_size_bytes_(uint8_t address_size_bytes) { this->address_size_bytes_ = address_size_bytes; } +void ExtEepromComponent::set_address_size_bytes_(uint8_t address_size_bytes) { + this->address_size_bytes_ = address_size_bytes; + } /// @brief Gets the number of bytes used for the address /// @return size in bytes uint8_t ExtEepromComponent::get_address_size_bytes_() { return this->address_size_bytes_; } From 9e6b5470a43f884761d01b6d8dd5a579ffa97b12 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 10 Feb 2024 11:59:19 +0000 Subject: [PATCH 14/20] and more why doesnt clang work in the dev container --- esphome/components/external_eeprom/external_eeprom.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 43b4e431c0..367f55e059 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -435,7 +435,7 @@ uint8_t ExtEepromComponent::get_page_write_time_() { return memory_page_write_ti /// @param address_bytes contains the number of bytes the device uses for address void ExtEepromComponent::set_address_size_bytes_(uint8_t address_size_bytes) { this->address_size_bytes_ = address_size_bytes; - } +} /// @brief Gets the number of bytes used for the address /// @return size in bytes uint8_t ExtEepromComponent::get_address_size_bytes_() { return this->address_size_bytes_; } From e56b1895230f18a16360b08c6bb03cbf57760f57 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Fri, 23 Feb 2024 19:16:13 +0000 Subject: [PATCH 15/20] Corrected buffer size and made consistant --- esphome/components/external_eeprom/external_eeprom.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 367f55e059..26d0a15f66 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -88,7 +88,7 @@ double ExtEepromComponent::read_double(uint32_t memaddr) { /// @param buffer_size is the size of the buffer and also the number of bytes to be read void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_size) { ESP_LOGVV(TAG, "Read %d bytes from address %d", buffer_size, memaddr); - uint32_t size = buffer_size; + uint16_t size = buffer_size; uint8_t *p = buff; i2c::ErrorCode ret; while (size >= 1) { @@ -203,7 +203,7 @@ void ExtEepromComponent::write_double(uint32_t memaddr, double value) { /// @param buffer_size is the size of the buffer and also the number of bytes to be written void ExtEepromComponent::write(uint32_t memaddr, uint8_t *data_to_write, uint16_t buffer_size) { ESP_LOGVV(TAG, "Write %d bytes to address %d", buffer_size, memaddr); - uint32_t size = buffer_size; + uint16_t size = buffer_size; uint8_t *p = data_to_write; // Check to make sure write is inside device range if (memaddr + buffer_size >= this->memory_size_bytes_) { From 7499053de913a928ffc4cf93a58d8d6293b7b31e Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Fri, 23 Feb 2024 19:34:01 +0000 Subject: [PATCH 16/20] Updated tests to new framework --- tests/components/external_eeprom/test.esp32.yaml | 11 +++++++++++ tests/components/external_eeprom/test.esp8266.yaml | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/components/external_eeprom/test.esp32.yaml create mode 100644 tests/components/external_eeprom/test.esp8266.yaml diff --git a/tests/components/external_eeprom/test.esp32.yaml b/tests/components/external_eeprom/test.esp32.yaml new file mode 100644 index 0000000000..006ef0f86b --- /dev/null +++ b/tests/components/external_eeprom/test.esp32.yaml @@ -0,0 +1,11 @@ +i2c: + - id: i2c_external_eeprom + sda: GPIO21 + scl: GPIO22 + +external_eeprom: + - id: ext_eeprom_component_1 + address: 0x57 + ee_memory_type: 24XX32 + i2c_buffer_size: 128 + i2c_id: i2c_external_eeprom diff --git a/tests/components/external_eeprom/test.esp8266.yaml b/tests/components/external_eeprom/test.esp8266.yaml new file mode 100644 index 0000000000..bac266c804 --- /dev/null +++ b/tests/components/external_eeprom/test.esp8266.yaml @@ -0,0 +1,10 @@ +i2c: + - id: i2c_external_eeprom + sda: D2 + scl: D1 + +external_eeprom: + - id: ext_eeprom_component_1 + address: 0x57 + ee_memory_type: 24XX32 + i2c_buffer_size: 128 From 0cbd7531baa2ec059be61e136caa426118c94eae Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 24 Feb 2024 08:44:29 +0000 Subject: [PATCH 17/20] Removed old tests --- tests/test1.yaml | 6 ------ tests/test3.1.yaml | 5 ----- tests/test5.yaml | 5 ----- 3 files changed, 16 deletions(-) diff --git a/tests/test1.yaml b/tests/test1.yaml index b1b2b31207..03ad69da50 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -4331,9 +4331,3 @@ alarm_control_panel: - lambda: !lambda |- ESP_LOGD("TEST", "State change %s", alarm_control_panel_state_to_string(id(alarmcontrolpanel1)->get_state())); -external_eeprom: - id: ext_eeprom_component_1 - address: 0x57 - ee_memory_type: 24XX32 - i2c_buffer_size: 128 - i2c_id: i2c_bus diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index 9399f9ad83..aa24edfcc1 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -733,8 +733,3 @@ adc128s102: allow_other_uses: true number: GPIO12 -external_eeprom: - id: ext_eeprom_component_1 - address: 0x57 - ee_memory_type: 24XX32 - i2c_buffer_size: 128 diff --git a/tests/test5.yaml b/tests/test5.yaml index 314545098a..bb28e0e3f0 100644 --- a/tests/test5.yaml +++ b/tests/test5.yaml @@ -731,8 +731,3 @@ light: bit1_high: 100us bit1_low: 100us -external_eeprom: - id: ext_eeprom_component_1 - address: 0x57 - ee_memory_type: 24XX32 - i2c_buffer_size: 128 From 2264c1b42d479a6552a367eeb58918031e131a46 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Sat, 24 Feb 2024 08:46:42 +0000 Subject: [PATCH 18/20] clean up extra line --- tests/test1.yaml | 1 - tests/test3.1.yaml | 1 - tests/test5.yaml | 1 - 3 files changed, 3 deletions(-) diff --git a/tests/test1.yaml b/tests/test1.yaml index 03ad69da50..3ca6faca8a 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -4330,4 +4330,3 @@ alarm_control_panel: then: - lambda: !lambda |- ESP_LOGD("TEST", "State change %s", alarm_control_panel_state_to_string(id(alarmcontrolpanel1)->get_state())); - diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index aa24edfcc1..b5428abbfa 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -732,4 +732,3 @@ adc128s102: cs_pin: allow_other_uses: true number: GPIO12 - diff --git a/tests/test5.yaml b/tests/test5.yaml index bb28e0e3f0..bf4247fb92 100644 --- a/tests/test5.yaml +++ b/tests/test5.yaml @@ -730,4 +730,3 @@ light: bit0_low: 100us bit1_high: 100us bit1_low: 100us - From eee3b5eebffbf4d0c919bc54d06fb8ea416e5399 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Fri, 8 Mar 2024 17:17:02 +0000 Subject: [PATCH 19/20] Resolved missing this's --- .../external_eeprom/external_eeprom.cpp | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 26d0a15f66..1ef0b8356c 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -138,9 +138,9 @@ void ExtEepromComponent::read(uint32_t memaddr, uint8_t *buff, uint16_t buffer_s /// @param str_to_read will hold the bytes read from the device on return of the fuction uint32_t ExtEepromComponent::read_string_from_eeprom(uint32_t memaddr, std::string &str_to_read) { - uint8_t new_str_len = read8(memaddr); + uint8_t new_str_len = this->read8(memaddr); uint8_t data[new_str_len + 1]; - read(memaddr + 1, (uint8_t *) data, new_str_len); + this->read(memaddr + 1, (uint8_t *) data, new_str_len); data[new_str_len] = '\0'; str_to_read = (char *) data; return memaddr + 1 + new_str_len; @@ -150,8 +150,8 @@ uint32_t ExtEepromComponent::read_string_from_eeprom(uint32_t memaddr, std::stri /// @param memaddr is the location to write /// @param data_to_write contains the byte to be written void ExtEepromComponent::write8(uint32_t memaddr, uint8_t data_to_write) { - if (read8(memaddr) != data_to_write) { // Update only if data is new - write(memaddr, &data_to_write, 1); + if (this->read8(memaddr) != data_to_write) { // Update only if data is new + this->write(memaddr, &data_to_write, 1); } } /// @brief Writes a 16 bit word to a given location @@ -159,7 +159,7 @@ void ExtEepromComponent::write8(uint32_t memaddr, uint8_t data_to_write) { /// @param memaddr is the location to write /// @param value contains the word to be written void ExtEepromComponent::write16(uint32_t memaddr, uint16_t value) { - if (read16(memaddr) != value) { // Update only if data is new + if (this->read16(memaddr) != value) { // Update only if data is new uint16_t val = value; this->write(memaddr, (uint8_t *) &val, sizeof(uint16_t)); } @@ -169,7 +169,7 @@ void ExtEepromComponent::write16(uint32_t memaddr, uint16_t value) { /// @param memaddr is the location to write /// @param value contains the word to be written void ExtEepromComponent::write32(uint32_t memaddr, uint32_t value) { - if (read32(memaddr) != value) { // Update only if data is new + if (this->read32(memaddr) != value) { // Update only if data is new uint32_t val = value; this->write(memaddr, (uint8_t *) &val, sizeof(uint32_t)); } @@ -179,7 +179,7 @@ void ExtEepromComponent::write32(uint32_t memaddr, uint32_t value) { /// @param memaddr is the location to write /// @param value contains the float to be written void ExtEepromComponent::write_float(uint32_t memaddr, float value) { - if (read_float(memaddr) != value) { // Update only if data is new + if (this->read_float(memaddr) != value) { // Update only if data is new float val = value; this->write(memaddr, (uint8_t *) &val, sizeof(float)); } @@ -189,7 +189,7 @@ void ExtEepromComponent::write_float(uint32_t memaddr, float value) { /// @param memaddr is the location to write /// @param value contains the double to be written void ExtEepromComponent::write_double(uint32_t memaddr, double value) { - if (read_double(memaddr) != value) // Update only if data is new + if (this->read_double(memaddr) != value) // Update only if data is new { double val = value; this->write(memaddr, (uint8_t *) &val, sizeof(double)); @@ -268,8 +268,8 @@ uint32_t ExtEepromComponent::write_string_to_eeprom(uint32_t memaddr, std::strin } uint8_t len = str_to_write.length(); const char *p = str_to_write.c_str(); - write8(memaddr, len); - write(memaddr + 1, (uint8_t *) p, len); + this->write8(memaddr, len); + this->write(memaddr + 1, (uint8_t *) p, len); return memaddr + 1 + len; } void ExtEepromComponent::dump_eeprom(uint32_t start_addr, uint16_t word_count) { @@ -311,7 +311,7 @@ void ExtEepromComponent::erase(uint8_t value_to_write) { temp_buffer[x] = value_to_write; for (uint32_t addr = 0; addr < this->get_memory_size_(); addr += this->memory_page_size_bytes_) - write(addr, temp_buffer, this->memory_page_size_bytes_); + this->write(addr, temp_buffer, this->memory_page_size_bytes_); } void ExtEepromComponent::set_memory_type(EEEDeviceType device_type) { device_type_ = device_type; @@ -384,10 +384,10 @@ void ExtEepromComponent::set_device_config_(uint32_t mem_size, uint8_t address_b /// @brief Sets the hw I2C buffer size -2, as 2 bytes are needed for control & addr /// @param buffer size in bytes, (ESP devices has a 128 I2C buffer so it is set to 126) -void ExtEepromComponent::set_i2c_buffer_size(uint8_t i2c_buffer_size) { i2c_buffer_size_ = i2c_buffer_size - 2; } +void ExtEepromComponent::set_i2c_buffer_size(uint8_t i2c_buffer_size) { this->i2c_buffer_size_ = i2c_buffer_size - 2; } /// @brief Gets the hw I2C buffer size -2, as 2 bytes are needed for control & addr /// @return buffer size in bytes -uint8_t ExtEepromComponent::get_i2c_buffer_size() { return i2c_buffer_size_; } +uint8_t ExtEepromComponent::get_i2c_buffer_size() { return this->i2c_buffer_size_; } // private functions void ExtEepromComponent::write_block_(uint8_t deviceaddr, uint32_t memaddr, const uint8_t *obj, uint8_t size) { @@ -415,22 +415,22 @@ void ExtEepromComponent::write_block_(uint8_t deviceaddr, uint32_t memaddr, cons } // @brief Sets the size of the device in bytes /// @param memSize contains the size of the device -void ExtEepromComponent::set_memory_size_(uint32_t mem_size) { memory_size_bytes_ = mem_size; } +void ExtEepromComponent::set_memory_size_(uint32_t mem_size) { this->memory_size_bytes_ = mem_size; } /// @brief Gets the user specified size of the device in bytes /// @return size in bytes -uint32_t ExtEepromComponent::get_memory_size_() { return memory_size_bytes_; } +uint32_t ExtEepromComponent::get_memory_size_() { return this->memory_size_bytes_; } /// @brief Sets the page size of the device in bytes /// @param page_size contains the size of the device pages -void ExtEepromComponent::set_page_size_(uint16_t page_size) { memory_page_size_bytes_ = page_size; } +void ExtEepromComponent::set_page_size_(uint16_t page_size) { this->memory_page_size_bytes_ = page_size; } /// @brief Gets the user specified size of the device pages in bytes /// @return Page size in bytes -uint16_t ExtEepromComponent::get_page_size_() { return memory_page_size_bytes_; } +uint16_t ExtEepromComponent::get_page_size_() { return this->memory_page_size_bytes_; } /// @brief Sets the page write for the device in ms /// @param write_time_ms contains the time to write a page of the device -void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { memory_page_write_time_ms_ = write_time_ms; } +void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { this->memory_page_write_time_ms_ = write_time_ms; } /// @brief Gets the user specified write time for a device page in ms /// @return page write time in ms -uint8_t ExtEepromComponent::get_page_write_time_() { return memory_page_write_time_ms_; } +uint8_t ExtEepromComponent::get_page_write_time_() { return this->memory_page_write_time_ms_; } /// @brief Set address_bytes for the device /// @param address_bytes contains the number of bytes the device uses for address void ExtEepromComponent::set_address_size_bytes_(uint8_t address_size_bytes) { From af9159f556de5b57901f56e7556d8dfe00539699 Mon Sep 17 00:00:00 2001 From: Pebblebed Date: Fri, 8 Mar 2024 17:22:53 +0000 Subject: [PATCH 20/20] fixed clang formatting --- esphome/components/external_eeprom/external_eeprom.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/external_eeprom/external_eeprom.cpp b/esphome/components/external_eeprom/external_eeprom.cpp index 1ef0b8356c..f5f6bbb646 100644 --- a/esphome/components/external_eeprom/external_eeprom.cpp +++ b/esphome/components/external_eeprom/external_eeprom.cpp @@ -427,7 +427,9 @@ void ExtEepromComponent::set_page_size_(uint16_t page_size) { this->memory_page_ uint16_t ExtEepromComponent::get_page_size_() { return this->memory_page_size_bytes_; } /// @brief Sets the page write for the device in ms /// @param write_time_ms contains the time to write a page of the device -void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { this->memory_page_write_time_ms_ = write_time_ms; } +void ExtEepromComponent::set_page_write_time_(uint8_t write_time_ms) { + this->memory_page_write_time_ms_ = write_time_ms; +} /// @brief Gets the user specified write time for a device page in ms /// @return page write time in ms uint8_t ExtEepromComponent::get_page_write_time_() { return this->memory_page_write_time_ms_; }