Proper way to keep values after reset

Hi there,

I would like to ask for best practice how it shall be use non-volatile data.

For example, keep data of integer or array type after reset.

I found way how to create txt file in USB mass storage, but this is quite overengineered solution, I think so.

Any idea for rookies like me ?

Thanks a lot.

MK.

Hi,

Just as an idea, most STM32 devices I know have backup registers that are kept as long as V_BAT is on. However there are not many registers usually. They can be found in the reference manuals under the tamper sections.

Best,
Chris

Hello,

use SD card or if you want it without external hardware try this - KVStore - API references and tutorials | Mbed OS 6 Documentation

BR, Jan

Thanks a lot.

is there any simpler than 200line example for KVStore?
MK

I don’t know about something official.
But you can try a simple code below.

For example click HERE
#include "mbed.h" //MbedOS 6.9 Nucleo-F303RE
#include "KVStore.h"
#include "kvstore_global_api.h"
#include <string>
/*A serial monitor is required (tested with Termite 3.4)*/
#define DEBUG 1

int main()
{
    debug_if(DEBUG, "Hello from Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
    thread_sleep_for(500);
    char str[5];  
    char kv_key[] = {"/kv/key"};
    char kv_value[5];
    kv_info_t info;
                
    while(true){
        debug_if(DEBUG, "Please, enter a value! - five-digit \n");
        scanf("%5s",str); 
        debug_if(DEBUG, "Your value is %s!\n", str);
        kv_get_info(kv_key, &info);
        kv_get(kv_key, kv_value, info.size, 0);
        bool check = true;
        for(int i=0;str[i]!='\0';i++){
            if(str[i]!=kv_value[i]) check = false; 
        }
        if(check){
            debug_if(DEBUG, "This value is already stored!\n");
        }else{
            debug_if(DEBUG, "Saving new value\n");
            kv_set(kv_key, str, strlen(str), 0);
        } 
    }
}

BR, Jan