diff --git a/README.md b/README.md index 287efa4..c88c77b 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Although the board shown in this examples is K64F, the example should work on an cd mbed-os-example-nvstore ``` - 2. Start with the default NVStore configuration, which uses the last two sectors (4KB each) as NVStore areas. + 2. Keep the default NVStore configuration, which uses the last two sectors (4KB each) as NVStore areas. 3. Compile and generate binary. @@ -48,15 +48,11 @@ Although the board shown in this examples is K64F, the example should work on an 7. The serial console should now display a series of results following the NVStore API invocations. - 8. Now copy ```mbed_app-8KB-areas.json``` to ```mbed_app.json```. This should use 8KB areas (two pairs of last sectors) as NVStore areas. + 8. Now copy `mbed_app-8KB-areas.json` to `mbed_app.json`. This should use 8KB areas (two pairs of last sectors) as NVStore areas. 9. Repeat steps 3-7. Notice the changes in prints of area addresses and sizes and with the amount of possible keys this configuration can hold. -10. Now copy ```mbed_app-disabled.json``` to ```mbed_app.json```. This disables NVStore. - -11. Repeat steps 3-7. Notice that the program now prints that NVStore is disabled. - -12. To restore default configuration, copy ```mbed_app-default-areas.json``` to ```mbed_app.json```. +10. To restore the default configuration, copy `mbed_app-default-areas.json` to `mbed_app.json`. ## Troubleshooting diff --git a/main.cpp b/main.cpp index 24f7091..6b40831 100644 --- a/main.cpp +++ b/main.cpp @@ -20,53 +20,10 @@ #include #include -#if NVSTORE_ENABLED -void display_return_code (int rc, int expected_rc) -{ - char ret_str[32]; - - switch (rc) { - case NVSTORE_SUCCESS : - strcpy(ret_str, "Success"); - break; - case NVSTORE_READ_ERROR : - strcpy(ret_str, "Read error"); - break; - case NVSTORE_WRITE_ERROR : - strcpy(ret_str, "Write error"); - break; - case NVSTORE_NOT_FOUND : - strcpy(ret_str, "Key not found"); - break; - case NVSTORE_DATA_CORRUPT : - strcpy(ret_str, "Data corrupt"); - break; - case NVSTORE_BAD_VALUE : - strcpy(ret_str, "Bad value"); - break; - case NVSTORE_BUFF_TOO_SMALL : - strcpy(ret_str, "Buffer too small"); - break; - case NVSTORE_FLASH_AREA_TOO_SMALL : - strcpy(ret_str, "Flash area too small"); - break; - case NVSTORE_OS_ERROR : - strcpy(ret_str, "OS error"); - break; - case NVSTORE_ALREADY_EXISTS : - strcpy(ret_str, "Key already exists"); - break; - } - printf("Return value is %s (%s)\n", - ret_str, (rc == expected_rc) ? "expected" : "NOT EXPECTED!"); -} -#endif - // Entry point for the example int main() { printf("\n--- Mbed OS NVStore example ---\n"); -#if NVSTORE_ENABLED uint16_t actual_len_bytes = 0; // NVStore is a sigleton, get its instance @@ -82,7 +39,7 @@ int main() { // Initialize NVstore. Note that it can be skipped, as it is lazily called by all other APIs rc = nvstore.init(); printf("Init NVStore. "); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // Show NVStore size, maximum number of keys and area addresses and sizes printf("NVStore size is %d\n", nvstore.size()); @@ -99,7 +56,7 @@ int main() { // Clear NVStore data. Should only be done once at factory configuration rc = nvstore.reset(); printf("Reset NVStore. "); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // Now set some values to the same key key = 1; @@ -107,32 +64,32 @@ int main() { value = 1000; rc = nvstore.set(key, sizeof(value), &value); printf("Set key %d to value %ld. ", key, value); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); value = 2000; rc = nvstore.set(key, sizeof(value), &value); printf("Set key %d to value %ld. ", key, value); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); value = 3000; rc = nvstore.set(key, sizeof(value), &value); printf("Set key %d to value %ld. ", key, value); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // Get the value of this key (should be 3000) rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes); printf("Get key %d. Value is %ld. ", key, value); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // Now remove the key rc = nvstore.remove(key); printf("Delete key %d. ", key); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // Get the key again, now it should not exist rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes); printf("Get key %d. ", key); - display_return_code(rc, NVSTORE_NOT_FOUND); + printf("Return code is %d\n", rc); key = 12; @@ -140,26 +97,23 @@ int main() { value = 50; rc = nvstore.set_once(key, sizeof(value), &value); printf("Set key %d once to value %ld. ", key, value); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // This should fail on key already existing value = 100; rc = nvstore.set(key, sizeof(value), &value); printf("Set key %d to value %ld. ", key, value); - display_return_code(rc, NVSTORE_ALREADY_EXISTS); + printf("Return code is %d\n", rc); // Get the value of this key (should be 50) rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes); printf("Get key %d. Value is %ld. ", key, value); - display_return_code(rc, NVSTORE_SUCCESS); + printf("Return code is %d\n", rc); // Get the data size for this key (should be 4) rc = nvstore.get_item_size(key, actual_len_bytes); printf("Data size for key %d is %d. ", key, actual_len_bytes); - display_return_code(rc, NVSTORE_SUCCESS); -#else - printf("\nNVStore is disabled!\n"); -#endif + printf("Return code is %d\n", rc); printf("\n--- Mbed OS NVStore example done. ---\n"); } diff --git a/mbed_app-disabled.json b/mbed_app-disabled.json deleted file mode 100644 index 6b80bc5..0000000 --- a/mbed_app-disabled.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "target_overrides": { - "*": { - "nvstore.enabled": false, - "platform.stdio-baud-rate": 115200, - "platform.default-serial-baud-rate": 115200 - } - } -}