Skip to content

Commit 715fc99

Browse files
committed
[PUYA] Check for PUYA chip as soon as possible at boot
Check for PUYA chip in call for `getFlashChipId()` This will only be done once and the result of the get function is also cached.
1 parent 0293a9e commit 715fc99

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

cores/esp8266/Esp.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ uint8_t EspClass::getCpuFreqMHz(void)
266266

267267
uint32_t EspClass::getFlashChipId(void)
268268
{
269-
return spi_flash_get_id();
269+
static uint32_t flash_chip_id = 0;
270+
if (flash_chip_id == 0) {
271+
flash_chip_id = spi_flash_get_id();
272+
// Check for PUYA chip and allocate buffer as soon as possible.
273+
flashIsPuya();
274+
}
275+
return flash_chip_id;
270276
}
271277

272278
uint32_t EspClass::getFlashChipRealSize(void)
@@ -569,8 +575,6 @@ bool EspClass::flashEraseSector(uint32_t sector) {
569575
return rc == 0;
570576
}
571577

572-
static uint32_t flash_chip_id = 0;
573-
574578
// PUYA flash chips need to read entire sector, update in memory and write sector again.
575579
static uint32_t *flash_write_puya_buf = 0;
576580

@@ -579,7 +583,7 @@ bool EspClass::flashIsPuya(){
579583
// Already detected PUYA and allocated buffer.
580584
return true;
581585
}
582-
bool isPuya = ((flash_chip_id & 0x000000ff) == 0x85); // 0x146085 PUYA
586+
bool isPuya = ((getFlashChipId() & 0x000000ff) == 0x85); // 0x146085 PUYA
583587
if (isPuya) {
584588
flash_write_puya_buf = (uint32_t*) malloc((SPI_FLASH_SEC_SIZE / 4) * sizeof(uint32_t));
585589
// No need to ever free this, since the flash chip will never change at runtime.
@@ -588,8 +592,6 @@ bool EspClass::flashIsPuya(){
588592
}
589593

590594
bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size) {
591-
if (flash_chip_id == 0)
592-
flash_chip_id = getFlashChipId();
593595
ets_isr_mask(FLASH_INT_MASK);
594596
int rc;
595597
uint32_t* ptr = data;

0 commit comments

Comments
 (0)