drivers/at24cxxx: add MTD wrapper for at24cxxx EEPROMs

This commit is contained in:
fabian18 2020-05-20 17:47:08 +02:00
parent f3dddd6127
commit 9ecd836443
5 changed files with 162 additions and 0 deletions

View File

@ -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

View File

@ -1 +1,5 @@
ifneq (,$(filter mtd_at24cxxx,$(USEMODULE)))
DIRS += mtd
endif
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,10 @@
MODULE := mtd_at24cxxx
BASE_MODULE := at24cxxx
# at24cxxx_mtd files
SRC := mtd.c
# enable submodules
SUBMODULES := 1
include $(RIOTBASE)/Makefile.base

View File

@ -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 <fabian.huessler@ovgu.de>
*
* @}
*/
#include <errno.h>
#include <assert.h>
#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
};

View File

@ -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 <fabian.huessler@ovgu.de>
*/
#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 */
/** @} */