Portenta H7 M7 Machine Control Board USB and Serial Port crashes when using KV_Store


I’m using the Arduino Portenta Machine Control Board (H7 M7) with the Arduino IDE and the Arduino mbed OS Portenta Boards (Version 4.0.10). The Mbed OS version is 6.17.0.

I want to use the KV_Store global static API to store some values.

However, when I try to use the KV_Store global static API, my board crashes. The serial port disappears from the serial monitor and I have to reset the serial port (clicking the board reset button twice) in order to upload another program. Here is the code I’m using.

Any help would be appreciated.

#include <Arduino.h>

#include "mbed.h" 
#include "KVStore.h"
#include "kvstore_global_api.h"
#include <string>

char str[5];  
char kv_key[] = {"/kv/key"};
char kv_value[5];
kv_info_t info;

void KV_Store_Init()
    Serial.print("Hello from Mbed OS version ");
    Serial.print("  ");
    Serial.print("  ");

void KV_Store_Run()
    strcpy(str, "05206");
    Serial.print("Your value is ");
    kv_get_info(kv_key, &info);
    kv_get(kv_key, kv_value, info.size, 0);

    Serial.println("Saving value");
    kv_set(kv_key, str, strlen(str), 0);

void setup()


void loop()
//  KV_Store_Init();


The crashes take place when I call any of the KV_Store global API functions:

    kv_get_info(kv_key, &info);

    kv_get(kv_key, kv_value, info.size, 0);

    kv_set(kv_key, str, strlen(str), 0);


according to your description it seems like the Serial is provided via USB interface and that sould be the reason why the serial port disappears.
A likely scenario:

  • KV trigger an exception because of an issue
  • Mbed OS will store some info about the exception and call MCU reset and that will cause the serial port disappears
  • After boot the MCU will stay in bootloader and print out MbedOS crash report to default Console, but you do not see this because you do not have access to it.

You need to check MbedOs crash report if that will say something.
You also should check if some KV methods do not return an error and that could be reason why another one trigger the exception.

// will be something similar
#define err_code(res) MBED_GET_ERROR_CODE(res)
int res;
if((res = kv_get_info(key, &info))!=0) printf("kv_get_info error: %d\n", err_code(res));
if ((res = kv_get(key, buffer, buffer_size, &actual_size, 0))!= 0) printf("kv_get() error: %d\n", err_code(res));
if ((res = kv_set(key, str, strlen(str), 0))!= 0) printf("kv_set() error: %d\n ", err_code(res));

PS: keep in mind the Portenta is Arduino board and all board specific issues is up to Arduino not to Mbed.

BR, Jan