Page 1 of 1

Flash write/read issue

Posted: Sat Apr 13, 2024 8:55 am
by Jimis1
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 1261 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 1261 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 1261 times

Re: Flash write/read issue

Posted: Sun Apr 14, 2024 12:59 am
by ESP_Sprite
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.

Re: Flash write/read issue

Posted: Sun Apr 14, 2024 8:08 am
by Jimis1
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 1107 times
Screenshot_2.jpg
Screenshot_2.jpg (33.13 KiB) Viewed 1107 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

Re: Flash write/read issue

Posted: Mon Apr 15, 2024 12:14 pm
by ESP_adokitkat
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.

Re: Flash write/read issue

Posted: Mon Apr 15, 2024 4:08 pm
by Jimis1
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 953 times

Re: Flash write/read issue

Posted: Mon Apr 15, 2024 4:15 pm
by Jimis1
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 942 times

Re: Flash write/read issue

Posted: Tue Apr 16, 2024 1:39 am
by ESP_Sprite
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.)