Skip to content

Commit

Permalink
Try #15380:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Mar 7, 2023
2 parents 777857a + 3f358ca commit 0be936c
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 225 deletions.
20 changes: 0 additions & 20 deletions cpu/esp_common/periph/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ extern uint32_t spi_flash_get_id(void);
/* forward declaration of mtd functions */
static int _flash_init(mtd_dev_t *dev);
static int _flash_read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size);
static int _flash_write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size);
static int _flash_write_page(mtd_dev_t *dev, const void *buff, uint32_t page,
uint32_t offset, uint32_t size);
static int _flash_erase(mtd_dev_t *dev, uint32_t addr, uint32_t size);
Expand Down Expand Up @@ -130,7 +129,6 @@ void spi_flash_drive_init(void)

_flash_driver.init = &_flash_init;
_flash_driver.read = &_flash_read;
_flash_driver.write = &_flash_write;
_flash_driver.write_page = &_flash_write_page;
_flash_driver.erase = &_flash_erase;
_flash_driver.power = &_flash_power;
Expand Down Expand Up @@ -311,24 +309,6 @@ static int _flash_read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
return (spi_flash_read(_flash_beg + addr, buff, size) == ESP_OK) ? 0 : -EIO;
}

static int _flash_write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
{
DEBUG("%s dev=%p addr=%08"PRIx32" size=%"PRIu32" buf=%p\n",
__func__, dev, addr, size, buff);

CHECK_PARAM_RET(dev == &_flash_dev, -ENODEV);
CHECK_PARAM_RET(buff != NULL, -ENOTSUP);

/* size must be within the flash address space */
CHECK_PARAM_RET(_flash_beg + addr + size <= _flash_end, -EOVERFLOW);

/* addr + size must be within a page */
CHECK_PARAM_RET(size <= _flashchip->page_size, -EOVERFLOW);
CHECK_PARAM_RET((addr % _flashchip->page_size) + size <= _flashchip->page_size, -EOVERFLOW);

return (spi_flash_write(_flash_beg + addr, buff, size) == ESP_OK) ? 0 : -EIO;
}

static int _flash_write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset,
uint32_t size)
{
Expand Down
30 changes: 0 additions & 30 deletions cpu/native/mtd/mtd_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,6 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
return (nread == size) ? 0 : -EIO;
}

static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
{
mtd_native_dev_t *_dev = (mtd_native_dev_t*) dev;
size_t mtd_size = dev->sector_count * dev->pages_per_sector * dev->page_size;

DEBUG("mtd_native: write from 0x%" PRIx32 " count %" PRIu32 "\n", addr, size);

if (addr + size > mtd_size) {
return -EOVERFLOW;
}
if (((addr % dev->page_size) + size) > dev->page_size) {
return -EOVERFLOW;
}

FILE *f = real_fopen(_dev->fname, "r+");
if (!f) {
return -EIO;
}
real_fseek(f, addr, SEEK_SET);
for (size_t i = 0; i < size; i++) {
uint8_t c = real_fgetc(f);
real_fseek(f, -1, SEEK_CUR);
real_fputc(c & ((uint8_t*)buff)[i], f);
}
real_fclose(f);

return 0;
}

static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset,
uint32_t size)
{
Expand Down Expand Up @@ -181,7 +152,6 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power)
const mtd_desc_t native_flash_driver = {
.read = _read,
.power = _power,
.write = _write,
.write_page = _write_page,
.erase = _erase,
.init = _init,
Expand Down
8 changes: 0 additions & 8 deletions drivers/at25xxx/mtd/mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ static int mtd_at25xxx_read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t
return at25xxx_read(mtd_at25xxx_->at25xxx_eeprom, addr, buff, size);
}

static int mtd_at25xxx_write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
{
DEBUG("[mtd_at25xxx] write: addr:%" PRIu32 " size:%" PRIu32 "\n", addr, size);
mtd_at25xxx_t *mtd_at25xxx_ = (mtd_at25xxx_t*)dev;
return at25xxx_write(mtd_at25xxx_->at25xxx_eeprom, addr, buff, size);
}

static int mtd_at25xxx_write_page(mtd_dev_t *dev, const void *src, uint32_t page, uint32_t offset,
uint32_t size)
{
Expand Down Expand Up @@ -87,7 +80,6 @@ static int mtd_at25xxx_power(mtd_dev_t *dev, enum mtd_power_state power)
const mtd_desc_t mtd_at25xxx_driver = {
.init = mtd_at25xxx_init,
.read = mtd_at25xxx_read,
.write = mtd_at25xxx_write,
.write_page = mtd_at25xxx_write_page,
.erase = mtd_at25xxx_erase,
.power = mtd_at25xxx_power,
Expand Down
19 changes: 0 additions & 19 deletions drivers/include/mtd.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,6 @@ struct mtd_desc {
uint32_t offset,
uint32_t size);

/**
* @brief Write to the Memory Technology Device (MTD)
*
* @p addr + @p size must be inside a page boundary. @p addr can be anywhere
* but the buffer cannot overlap two pages.
*
* @param[in] dev Pointer to the selected driver
* @param[in] buff Pointer to the data to be written
* @param[in] addr Starting address
* @param[in] size Number of bytes
*
* @return 0 on success
* @return < 0 value on error
*/
int (*write)(mtd_dev_t *dev,
const void *buff,
uint32_t addr,
uint32_t size);

/**
* @brief Write to the Memory Technology Device (MTD) using
* pagewise addressing.
Expand Down
17 changes: 0 additions & 17 deletions drivers/mtd/mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,6 @@ int mtd_write(mtd_dev_t *mtd, const void *src, uint32_t addr, uint32_t count)
return -ENODEV;
}

if (out_of_bounds(mtd, 0, addr, count)) {
return -EOVERFLOW;
}

if (mtd->driver->write) {
return mtd->driver->write(mtd, src, addr, count);
}

/* page size is always a power of two */
const uint32_t page_shift = bitarithm_msb(mtd->page_size);
const uint32_t page_mask = mtd->page_size - 1;
Expand Down Expand Up @@ -292,15 +284,6 @@ int mtd_write_page_raw(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t
return -EOVERFLOW;
}

if (mtd->driver->write_page == NULL) {
/* TODO: remove when all backends implement write_page */
if (mtd->driver->write) {
return mtd->driver->write(mtd, src, mtd->page_size * page + offset, count);
} else {
return -ENOTSUP;
}
}

/* Implementation assumes page size is <= INT_MAX and a power of two. */
/* We didn't find hardware yet where this is not true. */
assert(mtd->page_size <= INT_MAX);
Expand Down
16 changes: 0 additions & 16 deletions drivers/mtd_mapper/mtd_mapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,6 @@ static int _init(mtd_dev_t *mtd)
return res;
}

static int _write(mtd_dev_t *mtd, const void *src, uint32_t addr,
uint32_t count)
{
mtd_mapper_region_t *region = container_of(mtd, mtd_mapper_region_t, mtd);

if (addr + count > _region_size(region)) {
return -EOVERFLOW;
}

_lock(region);
int res = mtd_write(region->parent->mtd, src, addr + _byte_offset(region), count);
_unlock(region);
return res;
}

static int _write_page(mtd_dev_t *mtd, const void *src, uint32_t page,
uint32_t offset, uint32_t count)
{
Expand Down Expand Up @@ -199,7 +184,6 @@ const mtd_desc_t mtd_mapper_driver = {
.init = _init,
.read = _read,
.read_page = _read_page,
.write = _write,
.write_page = _write_page,
.erase = _erase,
.erase_sector = _erase_sector,
Expand Down
15 changes: 0 additions & 15 deletions drivers/mtd_sdcard/mtd_sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,10 @@ static int mtd_sdcard_read(mtd_dev_t *dev, void *buff, uint32_t addr,
return -EOVERFLOW;
}

static int mtd_sdcard_write(mtd_dev_t *dev, const void *buff, uint32_t addr,
uint32_t size)
{
int res = mtd_sdcard_write_page(dev, buff, addr / SD_HC_BLOCK_SIZE,
addr % SD_HC_BLOCK_SIZE, size);
if (res < 0) {
return res;
}
if (res == (int)size) {
return 0;
}
return -EOVERFLOW;
}

const mtd_desc_t mtd_sdcard_driver = {
.init = mtd_sdcard_init,
.read = mtd_sdcard_read,
.read_page = mtd_sdcard_read_page,
.write = mtd_sdcard_write,
.write_page = mtd_sdcard_write_page,
.erase_sector = mtd_sdcard_erase_sector,
.power = mtd_sdcard_power,
Expand Down
40 changes: 0 additions & 40 deletions drivers/mtd_spi_nor/mtd_spi_nor.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,45 +612,6 @@ static int mtd_spi_nor_read(mtd_dev_t *mtd, void *dest, uint32_t addr, uint32_t
return 0;
}

static int mtd_spi_nor_write(mtd_dev_t *mtd, const void *src, uint32_t addr, uint32_t size)
{
uint32_t total_size = mtd->page_size * mtd->pages_per_sector * mtd->sector_count;

DEBUG("mtd_spi_nor_write: %p, %p, 0x%" PRIx32 ", 0x%" PRIx32 "\n",
(void *)mtd, src, addr, size);
if (size == 0) {
return 0;
}
const mtd_spi_nor_t *dev = (mtd_spi_nor_t *)mtd;
if (size > mtd->page_size) {
DEBUG("mtd_spi_nor_write: ERR: page program >1 page (%" PRIu32 ")!\n", mtd->page_size);
return -EOVERFLOW;
}
if (dev->page_addr_mask &&
((addr & dev->page_addr_mask) != ((addr + size - 1) & dev->page_addr_mask))) {
DEBUG("mtd_spi_nor_write: ERR: page program spans page boundary!\n");
return -EOVERFLOW;
}
if (addr + size > total_size) {
return -EOVERFLOW;
}

mtd_spi_acquire(dev);

/* write enable */
mtd_spi_cmd(dev, dev->params->opcode->wren);

/* Page program */
mtd_spi_cmd_addr_write(dev, dev->params->opcode->page_program, addr, src, size);

/* waiting for the command to complete before returning */
wait_for_write_complete(dev, 0);

mtd_spi_release(dev);

return 0;
}

static int mtd_spi_nor_write_page(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t offset,
uint32_t size)
{
Expand Down Expand Up @@ -759,7 +720,6 @@ static int mtd_spi_nor_erase(mtd_dev_t *mtd, uint32_t addr, uint32_t size)
const mtd_desc_t mtd_spi_nor_driver = {
.init = mtd_spi_nor_init,
.read = mtd_spi_nor_read,
.write = mtd_spi_nor_write,
.write_page = mtd_spi_nor_write_page,
.erase = mtd_spi_nor_erase,
.power = mtd_spi_nor_power,
Expand Down
17 changes: 0 additions & 17 deletions tests/mtd_mapper/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,6 @@ static int _read_page(mtd_dev_t *dev, void *buff, uint32_t page, uint32_t offset
return size;
}

static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr,
uint32_t size)
{
(void)dev;

if (addr + size > sizeof(_dummy_memory)) {
return -EOVERFLOW;
}
if (size > PAGE_SIZE) {
return -EOVERFLOW;
}
memcpy(_dummy_memory + addr, buff, size);

return 0;
}

static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page, uint32_t offset, uint32_t size)
{
uint32_t addr = page * dev->page_size + offset;
Expand Down Expand Up @@ -168,7 +152,6 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power)
static const mtd_desc_t driver = {
.init = _init,
.read = _read,
.write = _write,
.erase = _erase,
.power = _power,
.read_page = _read_page,
Expand Down
15 changes: 5 additions & 10 deletions tests/pkg_littlefs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,14 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
return 0;
}

static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page,
uint32_t offset, uint32_t size)
{
(void)dev;
uint32_t addr = page * dev->page_size + offset;

if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
if (size > PAGE_SIZE) {
return -EOVERFLOW;
}
memcpy(dummy_memory + addr, buff, size);

return 0;
return size;
}

static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size)
Expand Down Expand Up @@ -105,7 +100,7 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power)
static const mtd_desc_t driver = {
.init = _init,
.read = _read,
.write = _write,
.write_page = _write_page,
.erase = _erase,
.power = _power,
};
Expand Down
15 changes: 5 additions & 10 deletions tests/pkg_littlefs2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,14 @@ static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
return 0;
}

static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
static int _write_page(mtd_dev_t *dev, const void *buff, uint32_t page,
uint32_t offset, uint32_t size)
{
(void)dev;
uint32_t addr = page * dev->page_size + offset;

if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
if (size > PAGE_SIZE) {
return -EOVERFLOW;
}
memcpy(dummy_memory + addr, buff, size);

return 0;
return size;
}

static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size)
Expand Down Expand Up @@ -105,7 +100,7 @@ static int _power(mtd_dev_t *dev, enum mtd_power_state power)
static const mtd_desc_t driver = {
.init = _init,
.read = _read,
.write = _write,
.write_page = _write_page,
.erase = _erase,
.power = _power,
};
Expand Down
Loading

0 comments on commit 0be936c

Please sign in to comment.