mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-17 05:12:57 +01:00
Merge pull request #15380 from benpicco/mtd_drop_write
mtd/*: drop .write() if .write_page() is implemented
This commit is contained in:
commit
6eac1e1761
@ -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);
|
||||
@ -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;
|
||||
@ -309,24 +307,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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
@ -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,
|
||||
|
@ -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)
|
||||
{
|
||||
@ -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,
|
||||
|
@ -250,25 +250,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
|
||||
*
|
||||
* @retval 0 on success
|
||||
* @retval <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.
|
||||
|
@ -169,14 +169,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;
|
||||
@ -255,6 +247,10 @@ int mtd_write_page(mtd_dev_t *mtd, const void *data, uint32_t page,
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (mtd->driver->write_page == NULL) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if (out_of_bounds(mtd, page, offset, len)) {
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
@ -297,12 +293,7 @@ int mtd_write_page_raw(mtd_dev_t *mtd, const void *src, uint32_t page, uint32_t
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
/* Implementation assumes page size is <= INT_MAX and a power of two. */
|
||||
|
@ -60,26 +60,6 @@ static int _read_page(mtd_dev_t *dev, void *dest,
|
||||
return size;
|
||||
}
|
||||
|
||||
static int _write(mtd_dev_t *dev, const void *src, uint32_t addr, uint32_t count)
|
||||
{
|
||||
mtd_emulated_t *mtd = (mtd_emulated_t *)dev;
|
||||
|
||||
(void)mtd;
|
||||
|
||||
assert(mtd);
|
||||
assert(src);
|
||||
|
||||
if (/* addr + count must be inside a page boundary. */
|
||||
(((addr % mtd->base.page_size) + count) > mtd->base.page_size) ||
|
||||
/* addr + count must not exceed the size of memory */
|
||||
((addr + count) > mtd->size)) {
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
memcpy(mtd->memory + addr, src, count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _write_page(mtd_dev_t *dev, const void *src,
|
||||
uint32_t page, uint32_t offset, uint32_t size)
|
||||
{
|
||||
@ -153,7 +133,6 @@ const mtd_desc_t _mtd_emulated_driver = {
|
||||
.init = _init,
|
||||
.read = _read,
|
||||
.read_page = _read_page,
|
||||
.write = _write,
|
||||
.write_page = _write_page,
|
||||
.erase = _erase,
|
||||
.erase_sector = _erase_sector,
|
||||
|
@ -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)
|
||||
{
|
||||
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -196,25 +196,10 @@ static int mtd_sdmmc_read(mtd_dev_t *dev, void *buff, uint32_t addr,
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
|
||||
static int mtd_sdmmc_write(mtd_dev_t *dev, const void *buff, uint32_t addr,
|
||||
uint32_t size)
|
||||
{
|
||||
int res = mtd_sdmmc_write_page(dev, buff, addr / SDMMC_SDHC_BLOCK_SIZE,
|
||||
addr % SDMMC_SDHC_BLOCK_SIZE, size);
|
||||
if (res < 0) {
|
||||
return res;
|
||||
}
|
||||
if (res == (int)size) {
|
||||
return 0;
|
||||
}
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
|
||||
const mtd_desc_t mtd_sdmmc_driver = {
|
||||
.init = mtd_sdmmc_init,
|
||||
.read = mtd_sdmmc_read,
|
||||
.read_page = mtd_sdmmc_read_page,
|
||||
.write = mtd_sdmmc_write,
|
||||
.write_page = mtd_sdmmc_write_page,
|
||||
.erase_sector = mtd_sdmmc_erase_sector,
|
||||
.power = mtd_sdmmc_power,
|
||||
|
@ -621,45 +621,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)
|
||||
{
|
||||
@ -768,7 +729,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,
|
||||
|
@ -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;
|
||||
@ -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,
|
||||
|
@ -146,7 +146,7 @@ static void test_mtd_write_read(void)
|
||||
uint8_t buf_page[page_size + 1];
|
||||
memset(buf_page, 1, sizeof(buf_page));
|
||||
ret = mtd_write(dev, buf_page, 0, sizeof(buf_page));
|
||||
TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret);
|
||||
TEST_ASSERT_EQUAL_INT(0, ret);
|
||||
|
||||
/* Read more than one page */
|
||||
ret = mtd_erase(dev, 0, dev->page_size * dev->pages_per_sector);
|
||||
@ -163,9 +163,9 @@ static void test_mtd_write_read(void)
|
||||
|
||||
/* pages overlap write */
|
||||
ret = mtd_write(dev, buf, dev->page_size - (sizeof(buf) / 2), sizeof(buf));
|
||||
TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret);
|
||||
TEST_ASSERT_EQUAL_INT(0, ret);
|
||||
ret = mtd_write(dev, buf_page, 1, sizeof(buf_page) - 1);
|
||||
TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret);
|
||||
TEST_ASSERT_EQUAL_INT(0, ret);
|
||||
}
|
||||
|
||||
#ifdef MTD_0
|
||||
|
Loading…
Reference in New Issue
Block a user