From 7884d6e7fe81bd7047175daf19f3176ae646052a Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 15 Feb 2023 22:19:22 +0100 Subject: [PATCH] drivers/at24cxxx: merge mtd driver with at24cxxx.c --- drivers/at24cxxx/Makefile | 4 -- drivers/at24cxxx/Makefile.include | 1 + drivers/at24cxxx/at24cxxx.c | 97 +++++++++++++++++++++++++------ drivers/at24cxxx/mtd/Makefile | 10 ---- drivers/at24cxxx/mtd/mtd.c | 80 ------------------------- drivers/include/at24cxxx.h | 17 ------ 6 files changed, 80 insertions(+), 129 deletions(-) delete mode 100644 drivers/at24cxxx/mtd/Makefile delete mode 100644 drivers/at24cxxx/mtd/mtd.c diff --git a/drivers/at24cxxx/Makefile b/drivers/at24cxxx/Makefile index 4c328975d2d6..48422e909a47 100644 --- a/drivers/at24cxxx/Makefile +++ b/drivers/at24cxxx/Makefile @@ -1,5 +1 @@ -ifneq (,$(filter mtd_at24cxxx,$(USEMODULE))) - DIRS += mtd -endif - include $(RIOTBASE)/Makefile.base diff --git a/drivers/at24cxxx/Makefile.include b/drivers/at24cxxx/Makefile.include index 111b7701a149..62ff78b0ccc3 100644 --- a/drivers/at24cxxx/Makefile.include +++ b/drivers/at24cxxx/Makefile.include @@ -1,4 +1,5 @@ PSEUDOMODULES += at24c% +PSEUDOMODULES += mtd_at24cxxx # handle at24cxxx being a distinct module NO_PSEUDOMODULES += at24cxxx diff --git a/drivers/at24cxxx/at24cxxx.c b/drivers/at24cxxx/at24cxxx.c index acbb27995d9a..b3b9e8824d8f 100644 --- a/drivers/at24cxxx/at24cxxx.c +++ b/drivers/at24cxxx/at24cxxx.c @@ -278,24 +278,6 @@ int at24cxxx_write(const at24cxxx_t *dev, uint32_t pos, const void *data, return check; } -int at24cxxx_write_page(const at24cxxx_t *dev, uint32_t page, uint32_t offset, - const void *data, size_t len) -{ - int check; - - assert(offset < DEV_PAGE_SIZE); - - /* write no more than to the end of the current page to prevent wrap-around */ - size_t remaining = DEV_PAGE_SIZE - offset; - len = MIN(len, remaining); - - i2c_acquire(DEV_I2C_BUS); - check = _write_page(dev, page * DEV_PAGE_SIZE + offset, data, len); - i2c_release(DEV_I2C_BUS); - - return check ? check : (int) len; -} - int at24cxxx_set(const at24cxxx_t *dev, uint32_t pos, uint8_t val, size_t len) { @@ -339,3 +321,82 @@ int at24cxxx_disable_write_protect(const at24cxxx_t *dev) gpio_clear(DEV_PIN_WP); return AT24CXXX_OK; } + +#ifdef MODULE_MTD_AT24CXXX +#include "mtd_at24cxxx.h" + +#define DEV(mtd_ptr) (((mtd_at24cxxx_t *)(mtd_ptr))->at24cxxx_eeprom) +#define PARAMS(mtd_ptr) (((mtd_at24cxxx_t *)(mtd_ptr))->params) + +static int _mtd_at24cxxx_init(mtd_dev_t *mtd) +{ + assert(mtd); + assert(mtd->driver == &mtd_at24cxxx_driver); + assert(DEV(mtd)); + assert(PARAMS(mtd)); + int init = at24cxxx_init(DEV(mtd), PARAMS(mtd)); + if (init != AT24CXXX_OK) { + return init; + } + mtd->page_size = DEV(mtd)->params.page_size; + mtd->pages_per_sector = 1; + mtd->sector_count = DEV(mtd)->params.eeprom_size / + DEV(mtd)->params.page_size; + mtd->write_size = 1; + return 0; +} + +static int _mtd_at24cxxx_read_page(mtd_dev_t *mtd, void *dest, uint32_t page, + uint32_t offset, uint32_t size) +{ + const at24cxxx_t *dev = DEV(mtd); + + /* some i2c implementations have a limit on the transfer size */ +#ifdef PERIPH_I2C_MAX_BYTES_PER_FRAME + size = MIN(size, PERIPH_I2C_MAX_BYTES_PER_FRAME); +#endif + + i2c_acquire(DEV_I2C_BUS); + int res = _read(dev, page * mtd->page_size + offset, dest, size); + i2c_release(DEV_I2C_BUS); + + return res < 0 ? res : (int)size; +} + +static int mtd_at24cxxx_write_page(mtd_dev_t *mtd, const void *src, uint32_t page, + uint32_t offset, uint32_t size) +{ + const at24cxxx_t *dev = DEV(mtd); + + /* write no more than to the end of the current page to prevent wrap-around */ + size_t remaining = DEV_PAGE_SIZE - offset; + size = MIN(size, remaining); + + i2c_acquire(DEV_I2C_BUS); + int res = _write_page(dev, page * mtd->page_size + offset, src, size); + i2c_release(DEV_I2C_BUS); + + return res < 0 ? res : (int)size; +} + +static int _mtd_at24cxxx_erase(mtd_dev_t *mtd, uint32_t addr, uint32_t size) +{ + return at24cxxx_clear(DEV(mtd), addr, size) == AT24CXXX_OK ? 0 : -EIO; +} + +static int _mtd_at24cxxx_power(mtd_dev_t *mtd, enum mtd_power_state power) +{ + (void)mtd; + (void)power; + return -ENOTSUP; +} + +const mtd_desc_t mtd_at24cxxx_driver = { + .init = _mtd_at24cxxx_init, + .read_page = _mtd_at24cxxx_read_page, + .write_page = mtd_at24cxxx_write_page, + .erase = _mtd_at24cxxx_erase, + .power = _mtd_at24cxxx_power, + .flags = MTD_DRIVER_FLAG_DIRECT_WRITE, +}; +#endif diff --git a/drivers/at24cxxx/mtd/Makefile b/drivers/at24cxxx/mtd/Makefile deleted file mode 100644 index b2910d512591..000000000000 --- a/drivers/at24cxxx/mtd/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -MODULE := mtd_at24cxxx -BASE_MODULE := at24cxxx - -# at24cxxx_mtd files -SRC := mtd.c - -# enable submodules -SUBMODULES := 1 - -include $(RIOTBASE)/Makefile.base diff --git a/drivers/at24cxxx/mtd/mtd.c b/drivers/at24cxxx/mtd/mtd.c deleted file mode 100644 index 984630d5c8f4..000000000000 --- a/drivers/at24cxxx/mtd/mtd.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - * - */ - -/** - * @ingroup drivers_at24cxxx - * @{ - * - * @file - * @brief MTD EEPROM driver implementation for at24cxxx EEPROM - * - * @author Fabian Hüßler - * - * @} - */ - -#include -#include - -#include "mtd_at24cxxx.h" - -#define DEV(mtd_ptr) (((mtd_at24cxxx_t *)(mtd_ptr))->at24cxxx_eeprom) -#define PARAMS(mtd_ptr) (((mtd_at24cxxx_t *)(mtd_ptr))->params) - -static int _mtd_at24cxxx_init(mtd_dev_t *mtd) -{ - assert(mtd); - assert(mtd->driver == &mtd_at24cxxx_driver); - assert(DEV(mtd)); - assert(PARAMS(mtd)); - int init = at24cxxx_init(DEV(mtd), PARAMS(mtd)); - if (init != AT24CXXX_OK) { - return init; - } - mtd->page_size = DEV(mtd)->params.page_size; - mtd->pages_per_sector = 1; - mtd->sector_count = DEV(mtd)->params.eeprom_size / - DEV(mtd)->params.page_size; - mtd->write_size = 1; - return 0; -} - -static int _mtd_at24cxxx_read_page(mtd_dev_t *mtd, void *dest, uint32_t page, - uint32_t offset, uint32_t size) -{ - int rc = at24cxxx_read(DEV(mtd), page * mtd->page_size + offset, dest, size); - return rc == AT24CXXX_OK ? (int)size : rc; -} - -static int mtd_at24cxxx_write_page(mtd_dev_t *mtd, const void *src, uint32_t page, - uint32_t offset, uint32_t size) -{ - return at24cxxx_write_page(DEV(mtd), page, offset, src, size); -} - -static int _mtd_at24cxxx_erase(mtd_dev_t *mtd, uint32_t addr, uint32_t size) -{ - return at24cxxx_clear(DEV(mtd), addr, size) == AT24CXXX_OK ? 0 : -EIO; -} - -static int _mtd_at24cxxx_power(mtd_dev_t *mtd, enum mtd_power_state power) -{ - (void)mtd; - (void)power; - return -ENOTSUP; -} - -const mtd_desc_t mtd_at24cxxx_driver = { - .init = _mtd_at24cxxx_init, - .read_page = _mtd_at24cxxx_read_page, - .write_page = mtd_at24cxxx_write_page, - .erase = _mtd_at24cxxx_erase, - .power = _mtd_at24cxxx_power, - .flags = MTD_DRIVER_FLAG_DIRECT_WRITE, -}; diff --git a/drivers/include/at24cxxx.h b/drivers/include/at24cxxx.h index 6c88482f23b3..7da67d2b7ee5 100644 --- a/drivers/include/at24cxxx.h +++ b/drivers/include/at24cxxx.h @@ -128,23 +128,6 @@ int at24cxxx_write_byte(const at24cxxx_t *dev, uint32_t pos, uint8_t data); int at24cxxx_write(const at24cxxx_t *dev, uint32_t pos, const void *data, size_t len); -/** - * @brief Sequentially write @p len bytes to a given @p page. - * The function will write up to the page boundary and then return - * the number of bytes written up to that. - * - * @param[in] dev AT24CXXX device handle - * @param[in] page page of EEPROM memory - * @param[in] offset offset from the start of the page, must be < page size - * @param[in] data write buffer - * @param[in] len requested length to be written - * - * @return number of bytes written on success - * @return error on failure - */ -int at24cxxx_write_page(const at24cxxx_t *dev, uint32_t page, uint32_t offset, - const void *data, size_t len); - /** * @brief Set @p len bytes from a given position @p pos to the * value @p val