Skip to content

drivers: mtd_spi_nor MTD interface driver for SPI NOR flash chips#6762

Merged
jnohlgard merged 2 commits into
RIOT-OS:masterfrom
OTAkeys:feat/mtd-spi-nor
Mar 30, 2017
Merged

drivers: mtd_spi_nor MTD interface driver for SPI NOR flash chips#6762
jnohlgard merged 2 commits into
RIOT-OS:masterfrom
OTAkeys:feat/mtd-spi-nor

Conversation

@vincent-d

Copy link
Copy Markdown
Member

This is based on #5668 with some minor improvements with erase and this has been ported to new spi interface.

@jnohlgard

Copy link
Copy Markdown
Member

There's an accidental autosave file included in this PR

@vincent-d

Copy link
Copy Markdown
Member Author

Fixed

@jnohlgard jnohlgard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found some minor comments. I will test the code on mulle later tonight or tomorrow

Comment thread boards/mulle/board.c Outdated
.private_data = &mulle_nvram_dev,
};

mtd_spi_nor_t mulle_nor_dev = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be static

Comment thread boards/mulle/Makefile.dep Outdated
USEMODULE += devfs
USEMODULE += mtd_spi_nor

#FEATURES_REQUIRED += periph_spi

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, already handled by drivers/Makefile.dep

Comment thread boards/mulle/board.c
mulle_nvram->write(mulle_nvram, &rec.u8[0], MULLE_NVRAM_BOOT_COUNT, sizeof(rec.u32));
}

int mulle_nor_init(void)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to add a call to this function from board_init

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread drivers/include/mtd_spi_nor.h Outdated
#define MTD_SPI_NOR_H_

#include <stdint.h>
#include <stdbool.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used include

Comment thread drivers/mtd_spi_nor/Makefile Outdated
@@ -0,0 +1,3 @@
MODULE = mtd_spi_nor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed when the directory name is the same as the module name

Comment thread drivers/mtd_spi_nor/mtd_spi_nor.c Outdated
break;
}
#if MODULE_XTIMER
xtimer_usleep(50 * US_PER_MS); /* TODO magic number */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to a macro define at the top of this file.

@jnohlgard

Copy link
Copy Markdown
Member

Needs rebase

@vincent-d

Copy link
Copy Markdown
Member Author

rebased

/* 4 KiO sectors can be erased with sector erase command */
mtd_spi_cmd_addr_write(dev, dev->opcode->block_erase_32k, addr_be, NULL, 0);
}
else {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size is not checked here. This causes the unit test for erase of invalid size to fail.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

* software bugs (the erase-all-your-files kind) */
DEBUG("addr = %" PRIx32 " ~dev->erase_addr_mask = %" PRIx32 "", addr, ~dev->sec_addr_mask);
DEBUG("mtd_spi_nor_erase: ERR: erase addr not aligned on %" PRIu32 " byte boundary.\n",
sector_size);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

riot/drivers/mtd_spi_nor/mtd_spi_nor.c:447:13: error: ‘sector_size’ undeclared (first use in this function)
             sector_size);
             ^

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

@jnohlgard jnohlgard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a small bug in mtd_spi_cmd_addr_write

Comment thread drivers/mtd_spi_nor/mtd_spi_nor.c Outdated
do {
/* Send opcode followed by address */
spi_transfer_byte(dev->spi, dev->cs, true, opcode);
spi_transfer_bytes(dev->spi, dev->cs, true, (char *)addr_buf, NULL, dev->addr_width);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently keeps the CS pin asserted on erase commands (count = 0), causes unit test failures on Mulle
Fixed:

        bool cont = (count > 0); /* only keep CS asserted when there is data that follows */
        spi_transfer_bytes(dev->spi, dev->cs, cont, (char *)addr_buf, NULL, dev->addr_width);
        /* Write data */
        if (cont) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks

@jnohlgard

Copy link
Copy Markdown
Member
mtd_tests.test_mtd_erase (tests/unittests/tests-mtd/tests-mtd.c 152) exp 0 was -139
    /* Erase 2nd - 3rd sector */
    ret = mtd_erase(dev, dev->pages_per_sector * dev->page_size,
                    dev->pages_per_sector * dev->page_size * 2);
    TEST_ASSERT_EQUAL_INT(0, ret);

erase only supports a single page in the current implementation

mtd_tests.test_mtd_write_read (tests/unittests/tests-mtd/tests-mtd.c 209) exp -139 was 9
    /* out of bounds write (addr) */
    ret = mtd_write(dev, buf, dev->pages_per_sector * dev->page_size * dev->sector_count,
                    sizeof(buf));
    TEST_ASSERT_EQUAL_INT(-EOVERFLOW, ret);

not sure what happens here.

@jnohlgard

Copy link
Copy Markdown
Member

jnohlgard@451af3d has some configuration for the Mulle MTD flash

Depends on #6810

@vincent-d

Copy link
Copy Markdown
Member Author
  • Fixed unittests. It passes on our hardware.
  • check bounds white write and erase

@jnohlgard

Copy link
Copy Markdown
Member

OK to squash

Joakim Nohlgård added 2 commits March 29, 2017 16:52
This is a generic SPI NOR flash driver which can be used with many
different flash chips.
@vincent-d

Copy link
Copy Markdown
Member Author

Squashed

@vincent-d vincent-d added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Mar 29, 2017

@jnohlgard jnohlgard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested working on Mulle, but only when #6810 is added.

@jnohlgard jnohlgard merged commit e9db827 into RIOT-OS:master Mar 30, 2017
@jnohlgard

Copy link
Copy Markdown
Member

@vincent-d do you mind approving #6810? It's a very small changeset

@vincent-d

Copy link
Copy Markdown
Member Author

\o/ Thanks @gebart

@vincent-d vincent-d deleted the feat/mtd-spi-nor branch March 30, 2017 09:54

@benpicco benpicco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I just discovered an old bug that was never noticed in the last 4 years 😨

.page_program = 0x02,
.sector_erase = 0x20,
.block_erase_32k = 0x52,
.block_erase = 0xd8,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 64KB BLOCK ERASE

else {
for (size_t i = 0; i < size / sector_size; i++) {
mtd_spi_cmd_addr_write(dev, dev->opcode->block_erase, addr_be, NULL, 0);
addr += sector_size;

@benpicco benpicco Oct 25, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and sector_size != 64k
So this will delete a lot more data than it should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants