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

Merge pull request #11081 from OTAkeys/pr/fix_mtd_spi_nor_read

drivers/mtd_spi_nor: fix read spanning pages
This commit is contained in:
benpicco 2020-05-06 19:51:16 +02:00 committed by GitHub
commit c7b15f4abe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -420,17 +420,9 @@ static int mtd_spi_nor_read(mtd_dev_t *mtd, void *dest, uint32_t addr, uint32_t
if (addr > chipsize) {
return -EOVERFLOW;
}
if (size > mtd->page_size) {
size = mtd->page_size;
}
if ((addr + size) > chipsize) {
size = chipsize - addr;
}
uint32_t page_addr_mask = dev->page_addr_mask;
if ((addr & page_addr_mask) != ((addr + size - 1) & page_addr_mask)) {
/* Reads across page boundaries must be split */
size = mtd->page_size - (addr & ~(page_addr_mask));
}
if (size == 0) {
return 0;
}

View File

@ -215,10 +215,24 @@ static void test_mtd_write_read(void)
/* out of bounds write (more than page size) */
const size_t page_size = dev->page_size;
const uint8_t buf_page[page_size + 1];
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);
/* Read more than one page */
ret = mtd_erase(dev, 0, dev->page_size * dev->pages_per_sector);
TEST_ASSERT_EQUAL_INT(0, ret);
ret = mtd_write(dev, buf_page, 0, dev->page_size);
TEST_ASSERT_EQUAL_INT(dev->page_size, ret);
ret = mtd_write(dev, buf_page, dev->page_size, dev->page_size);
TEST_ASSERT_EQUAL_INT(dev->page_size, ret);
memset(buf_page, 0, sizeof(buf_page));
ret = mtd_read(dev, buf_page, 0, sizeof(buf_page));
TEST_ASSERT_EQUAL_INT(sizeof(buf_page), ret);
TEST_ASSERT_EQUAL_INT(1, buf_page[0]);
TEST_ASSERT_EQUAL_INT(1, buf_page[sizeof(buf_page) - 1]);
/* pages overlap write */
ret = mtd_write(dev, buf, dev->page_size - (sizeof(buf) / 2), sizeof(buf));
TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret);