Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New flash writing method with offset/memory/size alignment handling #7514

Merged
merged 25 commits into from
Oct 15, 2020
Merged
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
701197d
Do not write more data than requested on PUYA flashes
drzony Aug 7, 2020
1def1c3
Merge branch 'master' into puya_flash_fixes
drzony Aug 9, 2020
02adea2
Always align flash reads/writes to 4 bytes
drzony Aug 10, 2020
f079f00
fixup! Always align flash reads/writes to 4 bytes
drzony Aug 10, 2020
e1cd190
fixup! Always align flash reads/writes to 4 bytes
drzony Aug 10, 2020
6cc5f1d
fixup! Always align flash reads/writes to 4 bytes
drzony Aug 11, 2020
7953559
Merge branch 'master' into puya_flash_fixes
drzony Aug 11, 2020
77da49c
Merge branch 'master' into puya_flash_fixes
drzony Aug 11, 2020
0a7b89a
Merge branch 'master' into puya_flash_fixes
drzony Oct 6, 2020
d941932
Check for result before additional read/write
drzony Oct 6, 2020
3cefcd9
Merge branch 'master' into puya_flash_fixes
drzony Oct 8, 2020
2bd885f
Add overloads for unaligned reads/writes
drzony Oct 8, 2020
d850f65
fixup! Add overloads for unaligned reads/writes
drzony Oct 8, 2020
5175867
fixup! Add overloads for unaligned reads/writes
drzony Oct 8, 2020
94c2df3
fixup! Add overloads for unaligned reads/writes
drzony Oct 8, 2020
64ceb92
fixup! Add overloads for unaligned reads/writes
drzony Oct 8, 2020
279a293
fixup! Add overloads for unaligned reads/writes
drzony Oct 9, 2020
c0d0257
fixup! Add overloads for unaligned reads/writes
drzony Oct 11, 2020
b3b3957
fixup! Add overloads for unaligned reads/writes
drzony Oct 11, 2020
a476e1b
Merge branch 'master' into puya_flash_fixes
drzony Oct 11, 2020
f2a6e0e
Add tests for flashRead/flashWrite
drzony Oct 11, 2020
eeb96fc
fixup! Add overloads for unaligned reads/writes
drzony Oct 12, 2020
7905a29
fixup! Add tests for flashRead/flashWrite
drzony Oct 12, 2020
323ffc0
fixup! Add tests for flashRead/flashWrite
drzony Oct 12, 2020
b919faa
fixup! Add overloads for unaligned reads/writes
drzony Oct 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,18 @@ bool EspClass::flashEraseSector(uint32_t sector) {
return rc == 0;
}

namespace {
#if PUYA_SUPPORT
#if PUYA_BUFFER_SIZE % 256 != 0
#error PUYA_BUFFER_SIZE is not 256 byte aligned
#endif
static SpiFlashOpResult spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
if (data == nullptr) {
return SPI_FLASH_RESULT_ERR;
}
if (size % 4 != 0) {
return SPI_FLASH_RESULT_ERR;
}
// PUYA flash chips need to read existing data, update in memory and write modified data again.
static uint32_t *flash_write_puya_buf = nullptr;

Expand All @@ -697,38 +704,62 @@ static SpiFlashOpResult spi_flash_write_puya(uint32_t offset, uint32_t *data, si
} else {
bytesLeft = 0;
}
size_t bytesAligned = (bytesNow + 3) & ~3;
rc = spi_flash_read(pos, flash_write_puya_buf, bytesAligned);
rc = spi_flash_read(pos, flash_write_puya_buf, bytesNow);
if (rc != SPI_FLASH_RESULT_OK) {
return rc;
}
for (size_t i = 0; i < bytesAligned / 4; ++i) {
for (size_t i = 0; i < bytesNow / 4; ++i) {
flash_write_puya_buf[i] &= *ptr;
++ptr;
}
rc = spi_flash_write(pos, flash_write_puya_buf, bytesAligned);
rc = spi_flash_write(pos, flash_write_puya_buf, bytesNow);
pos += bytesNow;
}
return rc;
}
#endif

static SpiFlashOpResult spi_flash_write_unaligned(uint32_t offset, uint32_t data, size_t remainder) {
uint32_t tempData;
SpiFlashOpResult rc = spi_flash_read(offset, &tempData, 4);
if (rc != SPI_FLASH_RESULT_OK) {
return rc;
}
for (int i = 4 - remainder; i < 4; i++) {
((uint8_t *)&data)[i] = 0xFF;
}
tempData &= data;
rc = spi_flash_write(offset, &tempData, 4);
return rc;
}
} // namespace

bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
size_t sizeAligned = size & ~3;
#if PUYA_SUPPORT
if (getFlashChipVendorId() == SPI_FLASH_VENDOR_PUYA) {
rc = spi_flash_write_puya(offset, data, size);
rc = spi_flash_write_puya(offset, data, sizeAligned);
}
else
#endif
{
rc = spi_flash_write(offset, data, size);
rc = spi_flash_write(offset, data, sizeAligned);
}
if (sizeAligned < size) {
rc = spi_flash_write_unaligned(offset + sizeAligned, data[sizeAligned / 4], size - sizeAligned);
}
return rc == SPI_FLASH_RESULT_OK;
}

bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
auto rc = spi_flash_read(offset, (uint32_t*) data, size);
size_t sizeAligned = size & ~3;
auto rc = spi_flash_read(offset, data, sizeAligned);
if (sizeAligned < size) {
uint32_t tempData;
rc = spi_flash_read(offset + sizeAligned, &tempData, 4);
memcpy((uint8_t *)data + sizeAligned, &tempData, size - sizeAligned);
}
return rc == SPI_FLASH_RESULT_OK;
}

Expand Down