1818 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1919 */
2020
21- #include " Arduino .h"
21+ #include " Esp .h"
2222#include " flash_utils.h"
2323#include " eboot_command.h"
2424#include < memory>
@@ -36,6 +36,14 @@ extern struct rst_info resetInfo;
3636
3737// #define DEBUG_SERIAL Serial
3838
39+ #ifndef PUYA_SUPPORT
40+ #define PUYA_SUPPORT 1
41+ #endif
42+ #ifndef PUYA_BUFFER_SIZE
43+ // Good alternative for buffer size is: SPI_FLASH_SEC_SIZE (= 4k)
44+ // Always use a multiple of flash page size (256 bytes)
45+ #define PUYA_BUFFER_SIZE 256
46+ #endif
3947
4048/* *
4149 * User-defined Literals
@@ -494,7 +502,7 @@ uint32_t EspClass::getSketchSize() {
494502
495503 image_header_t image_header;
496504 uint32_t pos = APP_START_OFFSET;
497- if (spi_flash_read (pos, (uint32_t *) &image_header, sizeof (image_header))) {
505+ if (spi_flash_read (pos, (uint32_t *) &image_header, sizeof (image_header)) != SPI_FLASH_RESULT_OK ) {
498506 return 0 ;
499507 }
500508 pos += sizeof (image_header);
@@ -506,7 +514,7 @@ uint32_t EspClass::getSketchSize() {
506514 ++section_index)
507515 {
508516 section_header_t section_header = {0 , 0 };
509- if (spi_flash_read (pos, (uint32_t *) §ion_header, sizeof (section_header))) {
517+ if (spi_flash_read (pos, (uint32_t *) §ion_header, sizeof (section_header)) != SPI_FLASH_RESULT_OK ) {
510518 return 0 ;
511519 }
512520 pos += sizeof (section_header);
@@ -578,26 +586,27 @@ bool EspClass::flashEraseSector(uint32_t sector) {
578586}
579587
580588#if PUYA_SUPPORT
581- static int spi_flash_write_puya (uint32_t offset, uint32_t *data, size_t size) {
589+ static SpiFlashOpResult spi_flash_write_puya (uint32_t offset, uint32_t *data, size_t size) {
582590 if (data == nullptr ) {
583- return 1 ; // SPI_FLASH_RESULT_ERR
591+ return SPI_FLASH_RESULT_ERR;
584592 }
585593 // PUYA flash chips need to read existing data, update in memory and write modified data again.
586594 static uint32_t *flash_write_puya_buf = nullptr ;
587- int rc = 0 ;
588- uint32_t * ptr = data;
589595
590596 if (flash_write_puya_buf == nullptr ) {
591597 flash_write_puya_buf = (uint32_t *) malloc (PUYA_BUFFER_SIZE);
592598 // No need to ever free this, since the flash chip will never change at runtime.
593599 if (flash_write_puya_buf == nullptr ) {
594600 // Memory could not be allocated.
595- return 1 ; // SPI_FLASH_RESULT_ERR
601+ return SPI_FLASH_RESULT_ERR;
596602 }
597603 }
604+
605+ SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
606+ uint32_t * ptr = data;
598607 size_t bytesLeft = size;
599608 uint32_t pos = offset;
600- while (bytesLeft > 0 && rc == 0 ) {
609+ while (bytesLeft > 0 && rc == SPI_FLASH_RESULT_OK ) {
601610 size_t bytesNow = bytesLeft;
602611 if (bytesNow > PUYA_BUFFER_SIZE) {
603612 bytesNow = PUYA_BUFFER_SIZE;
@@ -606,7 +615,7 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
606615 bytesLeft = 0 ;
607616 }
608617 rc = spi_flash_read (pos, flash_write_puya_buf, bytesNow);
609- if (rc != 0 ) {
618+ if (rc != SPI_FLASH_RESULT_OK ) {
610619 return rc;
611620 }
612621 for (size_t i = 0 ; i < bytesNow / 4 ; ++i) {
@@ -621,7 +630,7 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
621630#endif
622631
623632bool EspClass::flashWrite (uint32_t offset, uint32_t *data, size_t size) {
624- int rc = 0 ;
633+ SpiFlashOpResult rc = SPI_FLASH_RESULT_OK ;
625634#if PUYA_SUPPORT
626635 if (getFlashChipVendorId () == SPI_FLASH_VENDOR_PUYA) {
627636 rc = spi_flash_write_puya (offset, data, size);
@@ -631,12 +640,12 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
631640 {
632641 rc = spi_flash_write (offset, data, size);
633642 }
634- return rc == 0 ;
643+ return rc == SPI_FLASH_RESULT_OK ;
635644}
636645
637646bool EspClass::flashRead (uint32_t offset, uint32_t *data, size_t size) {
638- int rc = spi_flash_read (offset, (uint32_t *) data, size);
639- return rc == 0 ;
647+ auto rc = spi_flash_read (offset, (uint32_t *) data, size);
648+ return rc == SPI_FLASH_RESULT_OK ;
640649}
641650
642651String EspClass::getSketchMD5 ()
0 commit comments