Flash write/read issue

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Flash write/read issue

Postby Jimis1 » Sat Apr 13, 2024 8:55 am

Hi,

I have been banging my head over the following.

I have setup some functions to read and write to Flash which seem to work fine. But when I try to check the values of a global variable in flash compared to another global variable in RAM I have a problem. Before I even set the flash variable it is already updated.

In detail. The following code is run at boot once.
Global variables are set
uint8_t Test2;
uint8_t gg;
ScreenshotA.jpg
ScreenshotA.jpg (37.31 KiB) Viewed 1460 times
The result is this. Which is ok since Test2 has a value of 48 in flash from before.
Screenshot_9.jpg
Screenshot_9.jpg (30.52 KiB) Viewed 1460 times
Now if I remove the comment on //Test2=gg;
I get the this. Test2 has become =gg=50 without even reaching at this part of the code yet !! It's like it has been updated by magic! This happens all the time whatever value gg has, Test2 has already been updated with it !
Screenshot_11.jpg
Screenshot_11.jpg (35.23 KiB) Viewed 1460 times

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: Flash write/read issue

Postby ESP_Sprite » Sun Apr 14, 2024 12:59 am

Could it be that your code is running without you seeing it? Could be that between flashing and opening the monitor (which resets the processor), the CPU gets to execute code for a little bit.

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Flash write/read issue

Postby Jimis1 » Sun Apr 14, 2024 8:08 am

No, the code executes once before the main loop.
I realized I have this problem only in the first flash of the ESP. If I reset it works ok.
The following is between resets. This is repeating which is ok
Screenshot_1.jpg
Screenshot_1.jpg (34.38 KiB) Viewed 1306 times
Screenshot_2.jpg
Screenshot_2.jpg (33.13 KiB) Viewed 1306 times
Ofc this is not solving my problem cause it's the first flash that I'm trying to catch by reading flash if it's initialized or not

ESP_adokitkat
Posts: 50
Joined: Thu Jun 22, 2023 12:50 pm

Re: Flash write/read issue

Postby ESP_adokitkat » Mon Apr 15, 2024 12:14 pm

Hello.

Could you please try to declare those variables as volatile?

Code: Select all

volatile uint8_t Test2;
volatile uint8_t gg; 
Or try to use something like `vTaskDelay(pdMS_TO_TICKS(1000));` from `freertos/FreeRTOS.h` to delay the start.

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Flash write/read issue

Postby Jimis1 » Mon Apr 15, 2024 4:08 pm

Thanks ESP_adokitkat.

It worked with a delay 900ms not less ! The volatile didn't do anything.

There's clearly something very wrong here. I don't suppose I need to wait 900ms before I call my function!


Screenshot_3.jpg
Screenshot_3.jpg (73.44 KiB) Viewed 1152 times

Jimis1
Posts: 35
Joined: Wed May 03, 2023 6:20 am

Re: Flash write/read issue

Postby Jimis1 » Mon Apr 15, 2024 4:15 pm

ok found the problem.

I was trying to use the nvs functions too fast before they had time to initialize (facepalm), so I set the same delay right after them

Thanks a lot!


Screenshot_4.jpg
Screenshot_4.jpg (73.47 KiB) Viewed 1141 times

ESP_Sprite
Posts: 9725
Joined: Thu Nov 26, 2015 4:08 am

Re: Flash write/read issue

Postby ESP_Sprite » Tue Apr 16, 2024 1:39 am

That cannot be it; nvs initialization is not asynchroneous so nvs is available when the initialization routine finishes. My bet is still that your program runs for a small while (less than 900ms, seemingly) between the flashing being done and your serial monitor starting. Flashing resets the chip after it's done, monitor takes a few hundred ms to start and also resets the chip. So between the flashing and the first time you see text output, your program already ran once.

(Also, there's no need to make those variables volatile; all nvs calls are by definition sequence points, so there's no chance those variables will do something undefined. More generally, you're not doing interrupt or multithreading work here and the variables are in RAM and not hardware addresses, so 'volatile' is entirely unneeded.)

Who is online

Users browsing this forum: TRUEcabbage and 66 guests