18
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
19
*/
20
20
21
- #include " Arduino .h"
21
+ #include " Esp .h"
22
22
#include " flash_utils.h"
23
23
#include " eboot_command.h"
24
24
#include < memory>
@@ -36,6 +36,14 @@ extern struct rst_info resetInfo;
36
36
37
37
// #define DEBUG_SERIAL Serial
38
38
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
39
47
40
48
/* *
41
49
* User-defined Literals
@@ -494,7 +502,7 @@ uint32_t EspClass::getSketchSize() {
494
502
495
503
image_header_t image_header;
496
504
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 ) {
498
506
return 0 ;
499
507
}
500
508
pos += sizeof (image_header);
@@ -506,7 +514,7 @@ uint32_t EspClass::getSketchSize() {
506
514
++section_index)
507
515
{
508
516
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 ) {
510
518
return 0 ;
511
519
}
512
520
pos += sizeof (section_header);
@@ -578,26 +586,27 @@ bool EspClass::flashEraseSector(uint32_t sector) {
578
586
}
579
587
580
588
#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) {
582
590
if (data == nullptr ) {
583
- return 1 ; // SPI_FLASH_RESULT_ERR
591
+ return SPI_FLASH_RESULT_ERR;
584
592
}
585
593
// PUYA flash chips need to read existing data, update in memory and write modified data again.
586
594
static uint32_t *flash_write_puya_buf = nullptr ;
587
- int rc = 0 ;
588
- uint32_t * ptr = data;
589
595
590
596
if (flash_write_puya_buf == nullptr ) {
591
597
flash_write_puya_buf = (uint32_t *) malloc (PUYA_BUFFER_SIZE);
592
598
// No need to ever free this, since the flash chip will never change at runtime.
593
599
if (flash_write_puya_buf == nullptr ) {
594
600
// Memory could not be allocated.
595
- return 1 ; // SPI_FLASH_RESULT_ERR
601
+ return SPI_FLASH_RESULT_ERR;
596
602
}
597
603
}
604
+
605
+ SpiFlashOpResult rc = SPI_FLASH_RESULT_OK;
606
+ uint32_t * ptr = data;
598
607
size_t bytesLeft = size;
599
608
uint32_t pos = offset;
600
- while (bytesLeft > 0 && rc == 0 ) {
609
+ while (bytesLeft > 0 && rc == SPI_FLASH_RESULT_OK ) {
601
610
size_t bytesNow = bytesLeft;
602
611
if (bytesNow > PUYA_BUFFER_SIZE) {
603
612
bytesNow = PUYA_BUFFER_SIZE;
@@ -606,7 +615,7 @@ static int spi_flash_write_puya(uint32_t offset, uint32_t *data, size_t size) {
606
615
bytesLeft = 0 ;
607
616
}
608
617
rc = spi_flash_read (pos, flash_write_puya_buf, bytesNow);
609
- if (rc != 0 ) {
618
+ if (rc != SPI_FLASH_RESULT_OK ) {
610
619
return rc;
611
620
}
612
621
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) {
621
630
#endif
622
631
623
632
bool EspClass::flashWrite (uint32_t offset, uint32_t *data, size_t size) {
624
- int rc = 0 ;
633
+ SpiFlashOpResult rc = SPI_FLASH_RESULT_OK ;
625
634
#if PUYA_SUPPORT
626
635
if (getFlashChipVendorId () == SPI_FLASH_VENDOR_PUYA) {
627
636
rc = spi_flash_write_puya (offset, data, size);
@@ -631,12 +640,12 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
631
640
{
632
641
rc = spi_flash_write (offset, data, size);
633
642
}
634
- return rc == 0 ;
643
+ return rc == SPI_FLASH_RESULT_OK ;
635
644
}
636
645
637
646
bool 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 ;
640
649
}
641
650
642
651
String EspClass::getSketchMD5 ()
0 commit comments