I have run into a strange problem and was wondering if anyone has an idea or suggestion as to what is going wrong.
It is particularly strange as I had this working properly before. Below is a section of code, for readability reduced to the essential parts.
Code: Select all
espError = nvs_open_from_partition( "ParameterPart", "Parameters", NVS_READWRITE, &nvsHandle );
...
..
.
espError = nvs_get_u32 ( nvsHandle, ParameterList [ ParameterDataVersion ].Key , ( uint32_t * ) ParameterList [ ParameterDataVersion ].PointerToRamdata );
ESP_LOGI
(
TAG,
"Read version result: handle=%d, key=%s, version=%d, espError=%d",
nvsHandle,
ParameterList [ ParameterDataVersion ].Key,
*( uint32_t * ) ParameterList [ ParameterDataVersion ].PointerToRamdata,
espError
);
...
..
.
Pstor_SetAllStorageDatatoRamDefaults ( nvsHandle )
...
..
.
espError = nvs_set_u32 ( NvsHandle, key, *( uint32_t * ) dataLocation ); break;
...
..
.
ESP_LOGI
(
TAG,
"Stored Parameter with NvsHandle %d, index %d, key=%s to flash",
NvsHandle,
i,
key
);
Next I try to read the parameters data version using nvs_get_u32 followed by an ESP_LOGI operation to see the result of that. The output from which is:
<ESC>[0;32mI (1174) ParameterStorage.c: Read version result: handle=1, key=ParDataVersion , version=0, espError=4354<ESC>[0m
The next action is to call Pstor_SetAllStorageDatatoRamDefaults which will set all flash data to the defined default values as set up in the parameters' RAM image.
Within that function I use nvs_set_32 to actually write the parameter version data to the flash, it is the very last parameter I set.
Once nvs_set_32 returns OK I execute the next ESP_LOGI operation. The output of which is:
<ESC>[0;32mI (2117) ParameterStorage.c: Stored Parameter with NvsHandle 1, index 0, key=ParDataVersion to flash<ESC>[0m
The output indicates that I am using the exact same handle as before, the same key etc...
To the best of my knowledge the fact that I am writing data using that same handle with the nvs_set_... functions should take care of writing the parameters into the correct partition and namespace and if necessary also create the namespace when the first parameter is written to it.
The first time I get espError=4354 (ESP_ERR_NVS_NOT_FOUND) is 100% logical: initially the namespace and key/value pair do not exist and must be created. What is not logical is that for each parameter I write to flash the various nvs_set_... functions always report ESP_OK. That indicates the write operations work but when I try to read back the parameter version at every next start I get the same ESP_ERR_NVS_NOT_FOUND error again, on and on and on.
PS: I did not forget to use nvs_commit when necessary !