Skip to content

ESP32 Flash size read via API is wrong when flash is in modes qio, dio, qout #7157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jason2866 opened this issue Aug 20, 2022 · 2 comments · Fixed by #7159
Closed

ESP32 Flash size read via API is wrong when flash is in modes qio, dio, qout #7157

Jason2866 opened this issue Aug 20, 2022 · 2 comments · Fixed by #7159
Labels
Status: Awaiting triage Issue is waiting for triage
Milestone

Comments

@Jason2866
Copy link
Collaborator

Jason2866 commented Aug 20, 2022

Board

esp32 devkit 4 MB

Device Description

Plain Devkit board, nothing connected

Hardware Configuration

none

Version

v2.0.4

IDE Name

Platformio latest release

Operating System

macOS 12.5.1

Flash frequency

40 Mhz

PSRAM enabled

no

Upload speed

115200

Description

Get flash size correctly when in flash modes qio, qout, dio as it does when in flash mode dout.
The standard Arduino function get flash size is useless in most cases, since it just reads the values which are set from the IDE and it does not detect the real flash size

Sketch

Sketch to test https://github.com/Jason2866/ESP32_Show_Info
Can be run with Arduino IDE too.
The test sketch shows too that flash mode is not correct when queried via the API (qio and qout never shown)

Debug Message

First (correct) output in mode dout 

entry 0x400805c4
can't allocate memory with malloc

ESP32 SDK: v4.4.3
ESP32 CPU FREQ: 240 MHz
ESP32 APB FREQ: 80.0 MHz
ESP32 FLASH CHIP ID: 1458392
ESP32 FLASH REAL SIZE: 4.00 MB
ESP32 FLASH SIZE (MAGIC BYTE): 4.00 MB
ESP32 FLASH MODE: 3, 0=QIO, 1=QOUT, 2=DIO, 3=DOUT
ESP32 RAM SIZE: 366.19 KB
ESP32 FREE RAM: 342.56 KB
ESP32 MAX RAM ALLOC: 119.99 KB
ESP32 FREE PSRAM: 0.00 KB

Second (wrong) output in mode qio (shown wrong dio too)

entry 0x400805d0
can't allocate memory with malloc

ESP32 SDK: v4.4.3
ESP32 CPU FREQ: 240 MHz
ESP32 APB FREQ: 80.0 MHz
ESP32 FLASH CHIP ID: 729324
ESP32 FLASH REAL SIZE: 0.00 MB
ESP32 FLASH SIZE (MAGIC BYTE): 4.00 MB
ESP32 FLASH MODE: 2, 0=QIO, 1=QOUT, 2=DIO, 3=DOUT
ESP32 RAM SIZE: 366.19 KB
ESP32 FREE RAM: 342.56 KB
ESP32 MAX RAM ALLOC: 119.99 KB
ESP32 FREE PSRAM: 0.00 KB


### Other Steps to Reproduce

Running the example sketch

### I have checked existing issues, online documentation and the Troubleshooting Guide

- [X] I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@Jason2866 Jason2866 added the Status: Awaiting triage Issue is waiting for triage label Aug 20, 2022
@Jason2866 Jason2866 changed the title EPS32 Flash size read via API is wrong when flash is in modes qio, dio, qout ESP32 Flash size read via API is wrong when flash is in modes qio, dio, qout Aug 20, 2022
@TD-er
Copy link
Contributor

TD-er commented Aug 20, 2022

At boot you also can see the clock-div.
Would be nice if we could also get the runtime value to compute the actual flash frequency.

This function is also reading what's set during build:

uint32_t EspClass::getFlashChipSpeed(void)
{
    esp_image_header_t fhdr;
    if(flashRead(ESP_FLASH_IMAGE_BASE, (uint32_t*)&fhdr, sizeof(esp_image_header_t)) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
        return 0;
    }
    return magicFlashChipSpeed(fhdr.spi_speed);
}

@Jason2866
Copy link
Collaborator Author

Jason2866 commented Aug 21, 2022

@Staars did find a way. Credits for this go to him!

Correct Flash Chip ID can be read in all modes with:

uint32_t ESP_getFlashChipId(void)
{
  uint32_t id = g_rom_flashchip.device_id;
  id = ((id & 0xff) << 16) | ((id >> 16) & 0xff) | (id & 0xff00);
  return id;
}

AND when this is correctly fetched the flash size can be done :-)

Example to get Flash Chip ID and real Flash Chip size

#include <Arduino.h>

#if CONFIG_IDF_TARGET_ESP32
  #include "esp32/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32S2  // ESP32-S2
  #include "esp32s2/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32S3  // ESP32-S3
  #include "esp32s3/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32C3  // ESP32-C3
  #include "esp32c3/rom/spi_flash.h"
#endif

uint32_t ESP_getFlashChipId(void)
{
  uint32_t id = g_rom_flashchip.device_id;
  id = ((id & 0xff) << 16) | ((id >> 16) & 0xff) | (id & 0xff00);
  return id;
}

uint32_t ESP_getFlashChipRealSize(void)
{
  uint32_t id = (ESP_getFlashChipId() >> 16) & 0xFF;
  return 2 << (id - 1);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting triage Issue is waiting for triage
Projects
Development

Successfully merging a pull request may close this issue.

3 participants