Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/at24cxxx: merge mtd driver with at24cxxx.c #19281

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions drivers/at24cxxx/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
ifneq (,$(filter mtd_at24cxxx,$(USEMODULE)))
DIRS += mtd
endif

include $(RIOTBASE)/Makefile.base
1 change: 1 addition & 0 deletions drivers/at24cxxx/Makefile.include
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PSEUDOMODULES += at24c%
PSEUDOMODULES += mtd_at24cxxx
# handle at24cxxx being a distinct module
NO_PSEUDOMODULES += at24cxxx

Expand Down
97 changes: 79 additions & 18 deletions drivers/at24cxxx/at24cxxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
benpicco marked this conversation as resolved.
Show resolved Hide resolved
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,
benpicco marked this conversation as resolved.
Show resolved Hide resolved
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
10 changes: 0 additions & 10 deletions drivers/at24cxxx/mtd/Makefile

This file was deleted.

80 changes: 0 additions & 80 deletions drivers/at24cxxx/mtd/mtd.c

This file was deleted.

17 changes: 0 additions & 17 deletions drivers/include/at24cxxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
fabian18 marked this conversation as resolved.
Show resolved Hide resolved
const void *data, size_t len);

/**
* @brief Set @p len bytes from a given position @p pos to the
* value @p val
Expand Down