Skip to content

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

Merged
jnohlgard merged 2 commits intoRIOT-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 intoRIOT-OS:masterfrom
OTAkeys:feat/mtd-spi-nor

Conversation

@vincent-d
Copy link
Member

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

@jnohlgard
Copy link
Member

There's an accidental autosave file included in this PR

@vincent-d
Copy link
Member Author

Fixed

Copy link
Member

@jnohlgard jnohlgard left a comment

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

.private_data = &mulle_nvram_dev,
};

mtd_spi_nor_t mulle_nor_dev = {
Copy link
Member

Choose a reason for hiding this comment

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

Should be static

USEMODULE += devfs
USEMODULE += mtd_spi_nor

#FEATURES_REQUIRED += periph_spi
Copy link
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

mulle_nvram->write(mulle_nvram, &rec.u8[0], MULLE_NVRAM_BOOT_COUNT, sizeof(rec.u32));
}

int mulle_nor_init(void)
Copy link
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
Member Author

Choose a reason for hiding this comment

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

Done

#define MTD_SPI_NOR_H_

#include <stdint.h>
#include <stdbool.h>
Copy link
Member

Choose a reason for hiding this comment

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

Not used include

@@ -0,0 +1,3 @@
MODULE = mtd_spi_nor
Copy link
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

break;
}
#if MODULE_XTIMER
xtimer_usleep(50 * US_PER_MS); /* TODO magic number */
Copy link
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
Member

Needs rebase

@vincent-d
Copy link
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
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
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
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
Member Author

Choose a reason for hiding this comment

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

Addressed

Copy link
Member

@jnohlgard jnohlgard left a comment

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

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
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
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
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
Member

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

Depends on #6810

@vincent-d
Copy link
Member Author

  • Fixed unittests. It passes on our hardware.
  • check bounds white write and erase

@jnohlgard
Copy link
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
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
Copy link
Member

@jnohlgard jnohlgard left a comment

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
Member

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

@vincent-d
Copy link
Member Author

\o/ Thanks @gebart

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

@benpicco benpicco left a comment

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
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;
Copy link
Contributor

@benpicco benpicco Oct 25, 2020

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