Skip to content

Commit

Permalink
drivers/at24cxxx: merge mtd driver with at24cxxx.c
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Feb 16, 2023
1 parent 5667814 commit be4a0f5
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 129 deletions.
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_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_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_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_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_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_init,
.read_page = _mtd_read_page,
.write_page = _mtd_write_page,
.erase = _mtd_erase,
.power = _mtd_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,
const void *data, size_t len);

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

0 comments on commit be4a0f5

Please sign in to comment.