espressif / idf-flash-vendor-patches

Project to hold possible patches for flash vendors
Apache License 2.0
3 stars 2 forks source link

lock_sr: Cannot get proper flash_id in xmc_read_flash_id() (IDFGH-10714) #5

Open raomin opened 1 year ago

raomin commented 1 year ago

When I run the lock_sr fix, it wrongly returns 90200B as flash_id

[    63][I][xmc_lock.c:197] xmc_check_lock_sr(): non-xmc (90200B). SR lock skipped.

However, when I run esptool flash_id I get correctly XMC flash:

...
Stub running...
Manufacturer: 20
Device: 4017
Detected flash size: 8MB

Similarly, when I read flash_id from g_rom_flashchip:

    flash_id = g_rom_flashchip.device_id;
    log_i("chipid: %6x\n", flash_id);
    // doesn't work after this command:
    spi_flash_disable_interrupts_caches_and_other_cpu();

I properly get:

[    63][I][xmc_lock.c:90] xmc_read_flash_id(): chipid: 204017

Why isn't xmc_read_flash_id() working properly?

Note: My code is running on Arduino framework (framework-arduinoespressif32 @ 2.0.10), I've put it at the beginning of Setup(). My (wrong?) assumption is that it should work the same as in esp-idf...

raomin commented 1 year ago

@igrr could you please help on this one? I'm not very comfortable to skip the checks and force the SR lock with the similar functions that return wrong values.

ginkgm commented 1 year ago

Hi @raomin ,

I can't reproduce your issue on ESP32 with latest ESP-IDF. I find one weird place in your code: why do you need to call spi_flash_disable_interrupts_caches_and_other_cpu by yourself? xmc_read_flash_id is supposed to help you deal with the cache issue.

If you still meet this issue, please provide the complete code to reproduce the issue, as well as the IDF version, and your hardware details (ESP32, S2, or C3, etc.?)

raomin commented 1 year ago

Thanks @ginkgm for taking on this issue.

As explained I'm using Arduino framework (framework-arduinoespressif32 @ 2.0.10) and calling xmc_check_lock_sr from Setup(). I tested the code on espidf only and it works as expected. I just don't understand why arduino init would make xmc_read_flash_id() return wrong values...

The code you see was my attempt to identify why xmc_read_flash_id() was returning a wrong id. image

Hardware is esp32-wroom-32e

ginkgm commented 1 year ago

Hi @raomin ,

My colleagues provide this patch on Arduino, could you please have a try? He reads the same ID

XMC_Test.zip

raomin commented 1 year ago

Thanks @ginkgm!

Unfortunately, I have the same behavior.

Building:

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (6.3.2) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)       
PACKAGES:
 - framework-arduinoespressif32 @ 3.20009.0 (2.0.9)
 - tool-esptoolpy @ 1.40501.0 (4.5.1)
 - tool-mkfatfs @ 2.0.1
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 2.230.0 (2.30)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5

...

Running:

Hello world!
chipid: 204017
chipid: 90200b

Good one is the rom one. Matches esptool flash_id Manufacturer: 20 Device: 4017

raomin commented 1 year ago

I made another test using Arduino as a component of espidf.

platformio.ini

[env:esp32dev]
platform = https://github.com/tasmota/platform-espressif32#2023.07.00
board = esp32dev
framework = espidf
monitor_speed = 115200
build_flags = -D CONFIG_IDF_TARGET_ESP32 -D CONFIG_LOG_DEFAULT_LEVEL_INFO

main.cpp

#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "xmc_lock.h"
#include "rom/spi_flash.h"
#include "Arduino.h"

void setup() {
    // put your setup code here, to run once:
    printf("Hello world!\n");
    uint32_t flash_id = g_rom_flashchip.device_id;
    uint32_t sfdp_04, sfdp_06;
    printf("chipid from rom: %6"SCNx32"\n", flash_id);
    xmc_read_flash_id(&flash_id, (uint8_t*)&sfdp_04, (uint8_t*)&sfdp_06);
    printf("chipid within arduino: %6"SCNx32"\n", flash_id);
}

void loop() {
    // put your main code here, to run repeatedly:
    printf("loop\n");
    delay(1000);
}

packages (different versions from Arduino test)

PACKAGES:
 - framework-arduinoespressif32 @ 2.0.11
 - framework-espidf @ 3.40405.0 (4.4.5)
 - tool-cmake @ 3.21.3
 - tool-esptoolpy @ 1.40602.0 (4.6.2)
 - tool-idf @ 1.0.1
 - tool-mconf @ 1.4060000.20190628 (406.0.0)
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 2.230.0 (2.30)
 - tool-ninja @ 1.9.0
 - toolchain-esp32ulp @ 1.23500.220830 (2.35.0)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5

Run, all ok

Hello world!
chipid from rom: 204017
chipid within arduino: 204017

That's weird. esp-idf works, esp-idf with Arduino as component works, but Arduino doesn't ...

I'll try again Arduino...

raomin commented 1 year ago

I tried again Arduino, same as the one with Arduino as a component of espidf.

PACKAGES:
 - framework-arduinoespressif32 @ 2.0.11
 - tool-esptoolpy @ 1.40602.0 (4.6.2)
 - tool-mklittlefs @ 1.203.210628 (2.3)
 - tool-mkspiffs @ 2.230.0 (2.30)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5

Same result... two different IDs... 😢

�ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
Hello world!
chipid: 204017
chipid: 90200b

Any idea what could cause this to work on esp-idf and on esp-idf + Arduino as component but to fail on Arduino? Did your colleagues had a special configuration?

me-no-dev commented 1 year ago

@raomin there are a couple of things that come to mind, though I can not say what could be causing the issue.

Here is what I suggest:

raomin commented 1 year ago

Thanks @me-no-dev for stepping in!

Give us a screenshot of the board config menu in Arduino IDE, so that we can give you the sdkconfig used to build IDF.

I'm using PlatformIO with this as platform.ini:

[env:esp32dev]
platform = espressif32
board = esp32dev
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.11
framework = arduino

esp-idf + Arduino as component worked with minimal changes in sdkconfig (just some frequency for rtos irrc) But moving my whole project appears much more difficult with some errors at linking after importing libraries as components. So if I could get it working in Arduino, that would be best.

Thanks again!

me-no-dev commented 1 year ago

So if I could get it working in Arduino, that would be best.

Unfortunately I can not reproduce the problem in order to dig deeper, so I was hoping that you can do a minimal sketch with IDF and Arduino as component in order to track down the sdkconfig option that is causing the error. The sdkconfig you can start with is here

raomin commented 1 year ago

Ok, I'm on it now. I'm doing a first test with plain espidf in platformio and no arduino as component:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200

and the SDKConfig you indicated.

And that's enough to break it \o/

chipid: 90200b

I did a diff between the default sdkconfig and the one from arduino-esp32, there are quite some changes... so any idea what I should go to in the first place?

Also, is it enough for you to reproduce it? I zipped the project folder here if needed.

raomin commented 1 year ago

Just to confirm, I reverted the sdkconfig to the original one, cleaned and rebuild:

chipid: 204017

it does work as expected with the original sdkconfig.

me-no-dev commented 1 year ago

there are quite some changes...

Most are surely irrelevant. You can check things like NVS, PSRAM, spi_flash, app_rollback. Try with reverting some of those and see which one fixes the issue.

raomin commented 1 year ago

I found the following: When I copy the Serial flasher config section from yours, it breaks. Your sections sets flash frequency to 80m & dio (see below) mode when the default one is 40m and qio.

Frequency problem

So when I compile with flash frequency 40m it works. But when I compile with flash frequency 80m it doesn't Weirdly enough, when compiling with 80m and failing I read:

␛[0;32mI (22) boot.esp32: SPI Speed      : 40MHz␛[0m       
␛[0;32mI (26) boot.esp32: SPI Mode       : QIO␛[0m
␛[0;32mI (31) boot.esp32: SPI Flash Size : 4MB␛[0m

I see the same when compiling in 40mhz and with a proper result. BTW, I'm using an ESP32-WROOM-32E-N4

But still why a frequency difference in sdkconfig would still make the program work correctly but not reading specific registers? Can you reproduce if you select 80mhz in your IDE?

Side note:

I don't understand why in your sdkconfig I read:

CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
# CONFIG_ESPTOOLPY_FLASHMODE_DIO is not set
...
CONFIG_ESPTOOLPY_FLASHMODE="dio"
...
...
CONFIG_FLASHMODE_QIO=y
# CONFIG_FLASHMODE_QOUT is not set
# CONFIG_FLASHMODE_DIO is not set

isn't it antinomic? although for these settings I'm not sure what is taken from the sdkconfig vs IDE for the flashing only and what actually impacts the bootloader and firmware. Note, the default one is homogenous to qio in this respect.

Next steps

I'll set the board_build.f_flash = 40000000L on my Arduino project and see if it now properly detects chip id.

me-no-dev commented 1 year ago

isn't it antinomic?

ROM Bootloader can only work in DIO/DOUT modes, so images are started in DIO and the second stage bootloader switches actually to QIO/QOUT.

It work for me at 80MHz, I guess it's flash chip dependent? @ginkgm any clues why flash frequency can cause this?

@raomin does the issue show in ArduinoIDE if you select 40MHz?

raomin commented 1 year ago

I just installed Arduino IDE. In Arduino IDE, the code is working when I set 80m but not when I set 40m. 😮 40m: image 80m: image

I'll try again with esp-idf on PlatformIO but it seems it's the opposite of what I witnessed when configuring the frequency through the sdkconfig

raomin commented 1 year ago

I did more tests. Here is my current understanding:

  1. There are 2 places where the flash frequency is specified: In the sdkconfig (eg CONFIG_ESPTOOLPY_FLASHFREQ) and as a parameter of esptool.
  2. When setting up the value in sdkconfig it does not set the one of esptool.
  3. This leads to cases where the frequency in esptool and sdkconfig differs.
  4. When they differ, the chip_id returned is wrong, when they are the same, it's correct. This is regardless of the framework (espidf, Arduino or espidf+Arduino as component)

The only problem is that it makes NO sense to me... Why would the sdkconfig have a impact on the bootloader or firmware and not just on the default param for esptool? and why would this difference of frequency setting impact bootloader_read_flash_id?

raomin commented 1 year ago

@me-no-dev can you confirm it fails for you too when you set frequency to 40Mhz?

raomin commented 1 year ago

Right now, I don't really know what to think. Can I trust bootloader_execute_flash_command to properly write the status register if it fails to read it?

@me-no-dev. @ginkgm sorry to insist but that would greatly help us if you could find a way to address both cases.