Skip to content

Minor cleanups in ESPClass #6608

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

Merged
merged 4 commits into from
Oct 6, 2019
Merged
Changes from all commits
Commits
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
29 changes: 15 additions & 14 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ uint32_t EspClass::getSketchSize() {

image_header_t image_header;
uint32_t pos = APP_START_OFFSET;
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header))) {
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header)) != SPI_FLASH_RESULT_OK) {
return 0;
}
pos += sizeof(image_header);
Expand All @@ -506,7 +506,7 @@ uint32_t EspClass::getSketchSize() {
++section_index)
{
section_header_t section_header = {0, 0};
if (spi_flash_read(pos, (uint32_t*) &section_header, sizeof(section_header))) {
if (spi_flash_read(pos, (uint32_t*) &section_header, sizeof(section_header)) != SPI_FLASH_RESULT_OK) {
return 0;
}
pos += sizeof(section_header);
Expand Down Expand Up @@ -579,24 +579,25 @@ bool EspClass::flashEraseSector(uint32_t sector) {
#if PUYA_SUPPORT
static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
if (data == nullptr) {
return 1; // SPI_FLASH_RESULT_ERR
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;
int rc = 0;
uint32_t* ptr = data;

if (flash_write_puya_buf == nullptr) {
flash_write_puya_buf = (uint32_t*) malloc(PUYA_BUFFER_SIZE);
// No need to ever free this, since the flash chip will never change at runtime.
if (flash_write_puya_buf == nullptr) {
// Memory could not be allocated.
return 1; // SPI_FLASH_RESULT_ERR
return SPI_FLASH_RESULT_ERR;
}
}

SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
uint32_t* ptr = data;
size_t bytesLeft = size;
uint32_t pos = offset;
while (bytesLeft > 0 && rc == 0) {
while (bytesLeft > 0 && rc == SPI_FLASH_RESULT_OK) {
size_t bytesNow = bytesLeft;
if (bytesNow > PUYA_BUFFER_SIZE) {
bytesNow = PUYA_BUFFER_SIZE;
Expand All @@ -605,8 +606,8 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
bytesLeft = 0;
}
rc = spi_flash_read(pos, flash_write_puya_buf, bytesNow);
if (rc != 0) {
return rc;
if (rc != SPI_FLASH_RESULT_OK) {
return (int)rc;
}
for (size_t i = 0; i < bytesNow / 4; ++i) {
flash_write_puya_buf[i] &= *ptr;
Expand All @@ -615,12 +616,12 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
rc = spi_flash_write(pos, flash_write_puya_buf, bytesNow);
pos += bytesNow;
}
return rc;
return (int)rc;
}
#endif

bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
int rc = 0;
SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
#if PUYA_SUPPORT
if (getFlashChipVendorId() == SPI_FLASH_VENDOR_PUYA) {
rc = spi_flash_write_puya(offset, data, size);
Expand All @@ -630,12 +631,12 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
{
rc = spi_flash_write(offset, data, size);
}
return rc == 0;
return rc == SPI_FLASH_RESULT_OK;
}

bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
int rc = spi_flash_read(offset, (uint32_t*) data, size);
return rc == 0;
auto rc = spi_flash_read(offset, (uint32_t*) data, size);
return rc == SPI_FLASH_RESULT_OK;
}

String EspClass::getSketchMD5()
Expand Down