From 9ecd83644328dcbc25f6c873916dd717f75af67d Mon Sep 17 00:00:00 2001 From: fabian18 Date: Wed, 20 May 2020 17:47:08 +0200 Subject: [PATCH] drivers/at24cxxx: add MTD wrapper for at24cxxx EEPROMs --- drivers/Makefile.dep | 4 ++ drivers/at24cxxx/Makefile | 4 ++ drivers/at24cxxx/mtd/Makefile | 10 +++++ drivers/at24cxxx/mtd/mtd.c | 77 ++++++++++++++++++++++++++++++++++ drivers/include/mtd_at24cxxx.h | 67 +++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 drivers/at24cxxx/mtd/Makefile create mode 100644 drivers/at24cxxx/mtd/mtd.c create mode 100644 drivers/include/mtd_at24cxxx.h diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 7a833f4bc7..7a2fcca5fa 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -510,6 +510,10 @@ endif ifneq (,$(filter mtd_%,$(USEMODULE))) USEMODULE += mtd + ifneq (,$(filter mtd_at24cxxx,$(USEMODULE))) + USEMODULE += at24cxxx + endif + ifneq (,$(filter mtd_at25xxx,$(USEMODULE))) USEMODULE += at25xxx endif diff --git a/drivers/at24cxxx/Makefile b/drivers/at24cxxx/Makefile index 48422e909a..4c328975d2 100644 --- a/drivers/at24cxxx/Makefile +++ b/drivers/at24cxxx/Makefile @@ -1 +1,5 @@ +ifneq (,$(filter mtd_at24cxxx,$(USEMODULE))) + DIRS += mtd +endif + include $(RIOTBASE)/Makefile.base diff --git a/drivers/at24cxxx/mtd/Makefile b/drivers/at24cxxx/mtd/Makefile new file mode 100644 index 0000000000..b2910d5125 --- /dev/null +++ b/drivers/at24cxxx/mtd/Makefile @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000000..dd36e54a26 --- /dev/null +++ b/drivers/at24cxxx/mtd/mtd.c @@ -0,0 +1,77 @@ +/* + * 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; + return 0; +} + +static int _mtd_at24cxxx_read(mtd_dev_t *mtd, void *dest, uint32_t addr, + uint32_t size) +{ + return at24cxxx_read(DEV(mtd), addr, dest, size) == AT24CXXX_OK ? 0 : -EIO; +} + +static int _mtd_at24cxxx_write(mtd_dev_t *mtd, const void *src, uint32_t addr, + uint32_t size) +{ + return at24cxxx_write(DEV(mtd), addr, src, size) == AT24CXXX_OK ? 0 : -EIO; +} + +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 = _mtd_at24cxxx_read, + .write = _mtd_at24cxxx_write, + .erase = _mtd_at24cxxx_erase, + .power = _mtd_at24cxxx_power +}; diff --git a/drivers/include/mtd_at24cxxx.h b/drivers/include/mtd_at24cxxx.h new file mode 100644 index 0000000000..ba81401ac3 --- /dev/null +++ b/drivers/include/mtd_at24cxxx.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2020 Otto-von-Guericke Universität + * + * 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. + */ + +/** + * @defgroup drivers_mtd_at24cxxx MTD wrapper for AT24cxxx family of I2C-EEPROMs + * @ingroup drivers_storage + * @brief MTD wrapper for AT24XXX I2C based EEPROMs + * + * @{ + * + * @file + * @brief Interface definition for at24cxxx MTD wrapper + * + * + * @author Fabian Hüßler + */ + +#ifndef MTD_AT24CXXX_H +#define MTD_AT24CXXX_H + +#include "at24cxxx.h" +#include "mtd.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * @brief Device descriptor for mtd at24cxxx device + * + * This is an extension of the @c mtd_dev_t struct + */ +typedef struct { + mtd_dev_t base; /**< inherit from mtd_dev_t object */ + at24cxxx_t *at24cxxx_eeprom; /**< at24cxxx device descriptor */ + const at24cxxx_params_t* params; /**< at24cxxx parameters */ +} mtd_at24cxxx_t; + +/** + * @brief Shortcut initializer for @ref mtd_at24cxxx_t + */ +#define MTD_AT24CXXX_INIT(dev_p, params_p) \ +(mtd_at24cxxx_t) { \ + .base = { \ + .driver = &mtd_at24cxxx_driver \ + }, \ + .at24cxxx_eeprom = (dev_p), \ + .params = (params_p) \ +} + +/** + * @brief MTD EEPROM driver for at24cxxx EEPROM + */ +extern const mtd_desc_t mtd_at24cxxx_driver; + +#ifdef __cplusplus +} +#endif + +#endif /* MTD_AT24CXXX_H */ +/** @} */