Skip to content

Commit 49f0c98

Browse files
committed
Change non-volatile storage driver for nRF51 to use fstorage
pstorage is deprecated and no longer works in mbed OS 5.4 projects due to ARMmbed/mbed-os#4181. This updates the driver to use fstorage, and also makes the driver work on nRF52.
1 parent dc31ed2 commit 49f0c98

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

BLE_EddystoneService/source/PersistentStorageHelper/ConfigParamsPersistence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "ConfigParamsPersistence.h"
1818

19-
#ifndef TARGET_NRF51822 /* Persistent storage supported on nrf51 platforms */
19+
#if !defined(TARGET_NRF51822) && !defined(TARGET_NRF52832) /* Persistent storage supported on nrf51/nrf52 platforms */
2020
/**
2121
* When not using an nRF51-based target then persistent storage is not available.
2222
*/

BLE_EddystoneService/source/PersistentStorageHelper/nrfPersistentStorageHelper/nrfConfigParamsPersistence.cpp

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifdef TARGET_NRF51822 /* Persistent storage supported on nrf51 platforms */
17+
#if defined(TARGET_NRF51822) || defined(TARGET_NRF52832) /* Persistent storage supported on nrf51 and nrf52 platforms */
1818

1919
extern "C" {
20-
#include "pstorage.h"
20+
#include "fstorage.h"
2121
}
2222

2323
#include "nrf_error.h"
2424
#include "../../EddystoneService.h"
25+
#include <cstddef>
2526

2627
/**
2728
* Nordic specific structure used to store params persistently.
@@ -37,55 +38,52 @@ struct PersistentParams_t {
3738

3839
/**
3940
* The following is a module-local variable to hold configuration parameters for
40-
* short periods during flash access. This is necessary because the pstorage
41+
* short periods during flash access. This is necessary because the fstorage
4142
* APIs don't copy in the memory provided as data source. The memory cannot be
4243
* freed or reused by the application until this flash access is complete. The
4344
* load and store operations in this module initialize persistentParams and then
44-
* pass it on to the 'pstorage' APIs.
45+
* pass it on to the 'fstorage' APIs.
4546
*/
4647
static PersistentParams_t persistentParams;
4748

48-
static pstorage_handle_t pstorageHandle;
49-
5049
/**
51-
* Dummy callback handler needed by Nordic's pstorage module. This is called
50+
* Dummy callback handler needed by Nordic's fstorage module. This is called
5251
* after every flash access.
5352
*/
54-
static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
55-
uint8_t op_code,
56-
uint32_t result,
57-
uint8_t *p_data,
58-
uint32_t data_len)
53+
static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
5954
{
6055
/* Supress compiler warnings */
61-
(void) p_handle;
62-
(void) op_code;
56+
(void) evt;
6357
(void) result;
64-
(void) p_data;
65-
(void) data_len;
58+
}
59+
60+
FS_REGISTER_CFG(fs_config_t fs_config) = {
61+
NULL, // Begin pointer (set by fs_init)
62+
NULL, // End pointer (set by fs_init)
63+
&fs_evt_handler, // Function for event callbacks.
64+
1, // Number of physical flash pages required.
65+
0xFE // Priority for flash usage.
66+
};
6667

67-
/* APP_ERROR_CHECK(result); */
68+
69+
void loadPersistentParams(void) {
70+
// copy from flash into persistent params struct
71+
memcpy(&persistentParams, fs_config.p_start_addr, sizeof(PersistentParams_t));
6872
}
6973

7074
/* Platform-specific implementation for persistence on the nRF5x. Based on the
71-
* pstorage module provided by the Nordic SDK. */
75+
* fstorage module provided by the Nordic SDK. */
7276
bool loadEddystoneServiceConfigParams(EddystoneService::EddystoneParams_t *paramsP)
7377
{
74-
static bool pstorageInitied = false;
75-
if (!pstorageInitied) {
76-
pstorage_init();
77-
78-
static pstorage_module_param_t pstorageParams = {
79-
.cb = pstorageNotificationCallback,
80-
.block_size = sizeof(PersistentParams_t),
81-
.block_count = 1
82-
};
83-
pstorage_register(&pstorageParams, &pstorageHandle);
84-
pstorageInitied = true;
78+
static bool fstorageInited = false;
79+
if (!fstorageInited) {
80+
fs_init();
81+
fstorageInited = true;
8582
}
8683

87-
if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
88-
(persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
84+
loadPersistentParams();
85+
86+
if ((persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
8987
// On failure zero out and let the service reset to defaults
9088
memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t));
9189
return false;
@@ -96,22 +94,20 @@ bool loadEddystoneServiceConfigParams(EddystoneService::EddystoneParams_t *param
9694
}
9795

9896
/* Platform-specific implementation for persistence on the nRF5x. Based on the
99-
* pstorage module provided by the Nordic SDK. */
97+
* fstorage module provided by the Nordic SDK. */
10098
void saveEddystoneServiceConfigParams(const EddystoneService::EddystoneParams_t *paramsP)
10199
{
102100
memcpy(&persistentParams.params, paramsP, sizeof(EddystoneService::EddystoneParams_t));
103101
if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
104102
persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
105-
pstorage_store(&pstorageHandle,
106-
reinterpret_cast<uint8_t *>(&persistentParams),
107-
sizeof(PersistentParams_t),
108-
0 /* offset */);
109103
} else {
110-
pstorage_update(&pstorageHandle,
111-
reinterpret_cast<uint8_t *>(&persistentParams),
112-
sizeof(PersistentParams_t),
113-
0 /* offset */);
104+
fs_erase(&fs_config, fs_config.p_start_addr, sizeof(PersistentParams_t) / 4);
114105
}
106+
107+
fs_store(&fs_config,
108+
fs_config.p_start_addr,
109+
reinterpret_cast<uint32_t *>(&persistentParams),
110+
sizeof(PersistentParams_t) / 4);
115111
}
116112

117113
#endif /* #ifdef TARGET_NRF51822 */

0 commit comments

Comments
 (0)