Skip to content

Commit a7c4b26

Browse files
authored
Minor cleanups in ESPClass
Precursor to #6599
1 parent 08a0414 commit a7c4b26

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

cores/esp8266/Esp.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ uint32_t EspClass::getSketchSize() {
494494

495495
image_header_t image_header;
496496
uint32_t pos = APP_START_OFFSET;
497-
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header))) {
497+
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header)) == SPI_FLASH_RESULT_OK) {
498498
return 0;
499499
}
500500
pos += sizeof(image_header);
@@ -506,7 +506,7 @@ uint32_t EspClass::getSketchSize() {
506506
++section_index)
507507
{
508508
section_header_t section_header = {0, 0};
509-
if (spi_flash_read(pos, (uint32_t*) &section_header, sizeof(section_header))) {
509+
if (spi_flash_read(pos, (uint32_t*) &section_header, sizeof(section_header)) == SPI_FLASH_RESULT_OK) {
510510
return 0;
511511
}
512512
pos += sizeof(section_header);
@@ -579,24 +579,25 @@ bool EspClass::flashEraseSector(uint32_t sector) {
579579
#if PUYA_SUPPORT
580580
static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
581581
if (data == nullptr) {
582-
return 1; // SPI_FLASH_RESULT_ERR
582+
return SPI_FLASH_RESULT_ERR; // SPI_FLASH_RESULT_ERR
583583
}
584584
// PUYA flash chips need to read existing data, update in memory and write modified data again.
585585
static uint32_t *flash_write_puya_buf = nullptr;
586-
int rc = 0;
587-
uint32_t* ptr = data;
588586

589587
if (flash_write_puya_buf == nullptr) {
590588
flash_write_puya_buf = (uint32_t*) malloc(PUYA_BUFFER_SIZE);
591589
// No need to ever free this, since the flash chip will never change at runtime.
592590
if (flash_write_puya_buf == nullptr) {
593591
// Memory could not be allocated.
594-
return 1; // SPI_FLASH_RESULT_ERR
592+
return SPI_FLASH_RESULT_ERR; // SPI_FLASH_RESULT_ERR
595593
}
596594
}
595+
596+
SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
597+
uint32_t* ptr = data;
597598
size_t bytesLeft = size;
598599
uint32_t pos = offset;
599-
while (bytesLeft > 0 && rc == 0) {
600+
while (bytesLeft > 0 && rc == SPI_FLASH_RESULT_OK) {
600601
size_t bytesNow = bytesLeft;
601602
if (bytesNow > PUYA_BUFFER_SIZE) {
602603
bytesNow = PUYA_BUFFER_SIZE;
@@ -605,8 +606,8 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
605606
bytesLeft = 0;
606607
}
607608
rc = spi_flash_read(pos, flash_write_puya_buf, bytesNow);
608-
if (rc != 0) {
609-
return rc;
609+
if (rc != SPI_FLASH_RESULT_OK) {
610+
return (int)rc;
610611
}
611612
for (size_t i = 0; i < bytesNow / 4; ++i) {
612613
flash_write_puya_buf[i] &= *ptr;
@@ -615,12 +616,12 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
615616
rc = spi_flash_write(pos, flash_write_puya_buf, bytesNow);
616617
pos += bytesNow;
617618
}
618-
return rc;
619+
return (int)rc;
619620
}
620621
#endif
621622

622623
bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
623-
int rc = 0;
624+
SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
624625
#if PUYA_SUPPORT
625626
if (getFlashChipVendorId() == SPI_FLASH_VENDOR_PUYA) {
626627
rc = spi_flash_write_puya(offset, data, size);
@@ -630,12 +631,12 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
630631
{
631632
rc = spi_flash_write(offset, data, size);
632633
}
633-
return rc == 0;
634+
return rc == SPI_FLASH_RESULT_OK;
634635
}
635636

636637
bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size) {
637-
int rc = spi_flash_read(offset, (uint32_t*) data, size);
638-
return rc == 0;
638+
auto rc = spi_flash_read(offset, (uint32_t*) data, size);
639+
return rc == SPI_FLASH_RESULT_OK;
639640
}
640641

641642
String EspClass::getSketchMD5()

0 commit comments

Comments
 (0)