Skip to content

Commit 0293a9e

Browse files
committed
[PUYA] Only allocate memory when PUYA detected
core 2.5.0 PUYA patch, no puya: Description Function #calls call/sec min (ms) Avg (ms) max (ms) Save File 4 0.25 34.755 45.264 67.620 Free Mem: 16168 core 2.5.0 PUYA patch, Faked Puya detect: Description Function #calls call/sec min (ms) Avg (ms) max (ms) Save File 2 0.04 41.332 57.544 73.756 Free Mem: 11560
1 parent a835833 commit 0293a9e

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

cores/esp8266/Esp.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,20 @@ bool EspClass::flashEraseSector(uint32_t sector) {
571571

572572
static uint32_t flash_chip_id = 0;
573573

574+
// PUYA flash chips need to read entire sector, update in memory and write sector again.
575+
static uint32_t *flash_write_puya_buf = 0;
576+
574577
bool EspClass::flashIsPuya(){
575-
return ((flash_chip_id & 0x000000ff) == 0x85); // 0x146085 PUYA
578+
if (flash_write_puya_buf != 0) {
579+
// Already detected PUYA and allocated buffer.
580+
return true;
581+
}
582+
bool isPuya = ((flash_chip_id & 0x000000ff) == 0x85); // 0x146085 PUYA
583+
if (isPuya) {
584+
flash_write_puya_buf = (uint32_t*) malloc((SPI_FLASH_SEC_SIZE / 4) * sizeof(uint32_t));
585+
// No need to ever free this, since the flash chip will never change at runtime.
586+
}
587+
return isPuya;
576588
}
577589

578590
bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
@@ -581,17 +593,21 @@ bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
581593
ets_isr_mask(FLASH_INT_MASK);
582594
int rc;
583595
uint32_t* ptr = data;
596+
584597
if (flashIsPuya()) {
585-
static uint32_t read_buf[SPI_FLASH_SEC_SIZE / 4];
586-
rc = spi_flash_read(offset, read_buf, size);
598+
if (flash_write_puya_buf == 0) {
599+
// Memory could not be allocated.
600+
return false;
601+
}
602+
rc = spi_flash_read(offset, flash_write_puya_buf, size);
587603
if (rc != 0) {
588604
ets_isr_unmask(FLASH_INT_MASK);
589605
return false;
590606
}
591607
for (size_t i = 0; i < size / 4; ++i) {
592-
read_buf[i] &= data[i];
608+
flash_write_puya_buf[i] &= data[i];
593609
}
594-
ptr = read_buf;
610+
ptr = flash_write_puya_buf;
595611
}
596612
rc = spi_flash_write(offset, ptr, size);
597613
ets_isr_unmask(FLASH_INT_MASK);

0 commit comments

Comments
 (0)