@@ -266,7 +266,13 @@ uint8_t EspClass::getCpuFreqMHz(void)
266
266
267
267
uint32_t EspClass::getFlashChipId (void )
268
268
{
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;
270
276
}
271
277
272
278
uint32_t EspClass::getFlashChipRealSize (void )
@@ -569,8 +575,6 @@ bool EspClass::flashEraseSector(uint32_t sector) {
569
575
return rc == 0 ;
570
576
}
571
577
572
- static uint32_t flash_chip_id = 0 ;
573
-
574
578
// PUYA flash chips need to read entire sector, update in memory and write sector again.
575
579
static uint32_t *flash_write_puya_buf = 0 ;
576
580
@@ -579,7 +583,7 @@ bool EspClass::flashIsPuya(){
579
583
// Already detected PUYA and allocated buffer.
580
584
return true ;
581
585
}
582
- bool isPuya = ((flash_chip_id & 0x000000ff ) == 0x85 ); // 0x146085 PUYA
586
+ bool isPuya = ((getFlashChipId () & 0x000000ff ) == 0x85 ); // 0x146085 PUYA
583
587
if (isPuya) {
584
588
flash_write_puya_buf = (uint32_t *) malloc ((SPI_FLASH_SEC_SIZE / 4 ) * sizeof (uint32_t ));
585
589
// No need to ever free this, since the flash chip will never change at runtime.
@@ -588,8 +592,6 @@ bool EspClass::flashIsPuya(){
588
592
}
589
593
590
594
bool EspClass::flashWrite (uint32_t offset, uint32_t *data, size_t size) {
591
- if (flash_chip_id == 0 )
592
- flash_chip_id = getFlashChipId ();
593
595
ets_isr_mask (FLASH_INT_MASK);
594
596
int rc;
595
597
uint32_t * ptr = data;
0 commit comments