Skip to content

Commit

Permalink
Remove config file for disabled NVStore and return code translation f…
Browse files Browse the repository at this point in the history
…unction in code.
  • Loading branch information
David Saada committed Feb 28, 2018
1 parent e5f1359 commit 5a46c69
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 74 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
70 changes: 12 additions & 58 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,10 @@
#include <stdlib.h>
#include <stdio.h>

#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
Expand All @@ -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());
Expand All @@ -99,67 +56,64 @@ 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;

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;

// Now set another key once (it can't be set again)
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");
}
9 changes: 0 additions & 9 deletions mbed_app-disabled.json

This file was deleted.

0 comments on commit 5a46c69

Please sign in to comment.