1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/at24cxxx: merge mtd driver with at24cxxx.c

This commit is contained in:
Benjamin Valentin 2023-02-15 22:19:22 +01:00
parent fd47a1b7e4
commit 7884d6e7fe
6 changed files with 80 additions and 129 deletions

View File

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

View File

@ -1,4 +1,5 @@
PSEUDOMODULES += at24c%
PSEUDOMODULES += mtd_at24cxxx
# handle at24cxxx being a distinct module
NO_PSEUDOMODULES += at24cxxx

View File

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

View File

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

View File

@ -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 <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;
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,
};

View File

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