ST / Mbed OS mbed-os-example-ble-EddystoneService

This example is a fork of the following mbed-os example:

https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-EddystoneService/

Please read the documentation in this page.

Committer:
mbed_official
Date:
Fri May 19 17:30:11 2017 +0100
Revision:
31:f95fc2ac1dbc
Parent:
3:5120491ba317
Merge pull request #80 from janjongboom/patch-2

Change non-volatile storage driver for nRF51 to use fstorage
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-ble

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 3:5120491ba317 1 /* mbed Microcontroller Library
mbed_official 3:5120491ba317 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 3:5120491ba317 3 *
mbed_official 3:5120491ba317 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 3:5120491ba317 5 * you may not use this file except in compliance with the License.
mbed_official 3:5120491ba317 6 * You may obtain a copy of the License at
mbed_official 3:5120491ba317 7 *
mbed_official 3:5120491ba317 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 3:5120491ba317 9 *
mbed_official 3:5120491ba317 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 3:5120491ba317 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 3:5120491ba317 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 3:5120491ba317 13 * See the License for the specific language governing permissions and
mbed_official 3:5120491ba317 14 * limitations under the License.
mbed_official 3:5120491ba317 15 */
mbed_official 3:5120491ba317 16
mbed_official 31:f95fc2ac1dbc 17 #if defined(TARGET_NRF51822) || defined(TARGET_NRF52832) /* Persistent storage supported on nrf51 and nrf52 platforms */
mbed_official 3:5120491ba317 18
mbed_official 3:5120491ba317 19 extern "C" {
mbed_official 31:f95fc2ac1dbc 20 #include "fstorage.h"
mbed_official 3:5120491ba317 21 }
mbed_official 3:5120491ba317 22
mbed_official 3:5120491ba317 23 #include "nrf_error.h"
mbed_official 3:5120491ba317 24 #include "../../EddystoneService.h"
mbed_official 31:f95fc2ac1dbc 25 #include <cstddef>
mbed_official 3:5120491ba317 26
mbed_official 3:5120491ba317 27 /**
mbed_official 3:5120491ba317 28 * Nordic specific structure used to store params persistently.
mbed_official 3:5120491ba317 29 * It extends EddystoneService::EddystoneParams_t with a persistence signature.
mbed_official 3:5120491ba317 30 */
mbed_official 3:5120491ba317 31 struct PersistentParams_t {
mbed_official 3:5120491ba317 32 EddystoneService::EddystoneParams_t params;
mbed_official 3:5120491ba317 33 uint32_t persistenceSignature; /* This isn't really a parameter, but having the expected
mbed_official 3:5120491ba317 34 * magic value in this field indicates persistence. */
mbed_official 3:5120491ba317 35
mbed_official 3:5120491ba317 36 static const uint32_t MAGIC = 0x1BEAC000; /* Magic that identifies persistence */
mbed_official 3:5120491ba317 37 };
mbed_official 3:5120491ba317 38
mbed_official 3:5120491ba317 39 /**
mbed_official 3:5120491ba317 40 * The following is a module-local variable to hold configuration parameters for
mbed_official 31:f95fc2ac1dbc 41 * short periods during flash access. This is necessary because the fstorage
mbed_official 3:5120491ba317 42 * APIs don't copy in the memory provided as data source. The memory cannot be
mbed_official 3:5120491ba317 43 * freed or reused by the application until this flash access is complete. The
mbed_official 3:5120491ba317 44 * load and store operations in this module initialize persistentParams and then
mbed_official 31:f95fc2ac1dbc 45 * pass it on to the 'fstorage' APIs.
mbed_official 3:5120491ba317 46 */
mbed_official 3:5120491ba317 47 static PersistentParams_t persistentParams;
mbed_official 3:5120491ba317 48
mbed_official 3:5120491ba317 49 /**
mbed_official 31:f95fc2ac1dbc 50 * Dummy callback handler needed by Nordic's fstorage module. This is called
mbed_official 3:5120491ba317 51 * after every flash access.
mbed_official 3:5120491ba317 52 */
mbed_official 31:f95fc2ac1dbc 53 static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
mbed_official 3:5120491ba317 54 {
mbed_official 3:5120491ba317 55 /* Supress compiler warnings */
mbed_official 31:f95fc2ac1dbc 56 (void) evt;
mbed_official 3:5120491ba317 57 (void) result;
mbed_official 31:f95fc2ac1dbc 58 }
mbed_official 3:5120491ba317 59
mbed_official 31:f95fc2ac1dbc 60 FS_REGISTER_CFG(fs_config_t fs_config) = {
mbed_official 31:f95fc2ac1dbc 61 NULL, // Begin pointer (set by fs_init)
mbed_official 31:f95fc2ac1dbc 62 NULL, // End pointer (set by fs_init)
mbed_official 31:f95fc2ac1dbc 63 &fs_evt_handler, // Function for event callbacks.
mbed_official 31:f95fc2ac1dbc 64 1, // Number of physical flash pages required.
mbed_official 31:f95fc2ac1dbc 65 0xFE // Priority for flash usage.
mbed_official 31:f95fc2ac1dbc 66 };
mbed_official 31:f95fc2ac1dbc 67
mbed_official 31:f95fc2ac1dbc 68
mbed_official 31:f95fc2ac1dbc 69 void loadPersistentParams(void) {
mbed_official 31:f95fc2ac1dbc 70 // copy from flash into persistent params struct
mbed_official 31:f95fc2ac1dbc 71 memcpy(&persistentParams, fs_config.p_start_addr, sizeof(PersistentParams_t));
mbed_official 3:5120491ba317 72 }
mbed_official 3:5120491ba317 73
mbed_official 3:5120491ba317 74 /* Platform-specific implementation for persistence on the nRF5x. Based on the
mbed_official 31:f95fc2ac1dbc 75 * fstorage module provided by the Nordic SDK. */
mbed_official 3:5120491ba317 76 bool loadEddystoneServiceConfigParams(EddystoneService::EddystoneParams_t *paramsP)
mbed_official 3:5120491ba317 77 {
mbed_official 31:f95fc2ac1dbc 78 static bool fstorageInited = false;
mbed_official 31:f95fc2ac1dbc 79 if (!fstorageInited) {
mbed_official 31:f95fc2ac1dbc 80 fs_init();
mbed_official 31:f95fc2ac1dbc 81 fstorageInited = true;
mbed_official 3:5120491ba317 82 }
mbed_official 3:5120491ba317 83
mbed_official 31:f95fc2ac1dbc 84 loadPersistentParams();
mbed_official 31:f95fc2ac1dbc 85
mbed_official 31:f95fc2ac1dbc 86 if ((persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
mbed_official 3:5120491ba317 87 // On failure zero out and let the service reset to defaults
mbed_official 3:5120491ba317 88 memset(paramsP, 0, sizeof(EddystoneService::EddystoneParams_t));
mbed_official 3:5120491ba317 89 return false;
mbed_official 3:5120491ba317 90 }
mbed_official 3:5120491ba317 91
mbed_official 3:5120491ba317 92 memcpy(paramsP, &persistentParams.params, sizeof(EddystoneService::EddystoneParams_t));
mbed_official 3:5120491ba317 93 return true;
mbed_official 3:5120491ba317 94 }
mbed_official 3:5120491ba317 95
mbed_official 3:5120491ba317 96 /* Platform-specific implementation for persistence on the nRF5x. Based on the
mbed_official 31:f95fc2ac1dbc 97 * fstorage module provided by the Nordic SDK. */
mbed_official 3:5120491ba317 98 void saveEddystoneServiceConfigParams(const EddystoneService::EddystoneParams_t *paramsP)
mbed_official 3:5120491ba317 99 {
mbed_official 3:5120491ba317 100 memcpy(&persistentParams.params, paramsP, sizeof(EddystoneService::EddystoneParams_t));
mbed_official 3:5120491ba317 101 if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
mbed_official 3:5120491ba317 102 persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
mbed_official 3:5120491ba317 103 } else {
mbed_official 31:f95fc2ac1dbc 104 fs_erase(&fs_config, fs_config.p_start_addr, sizeof(PersistentParams_t) / 4);
mbed_official 3:5120491ba317 105 }
mbed_official 31:f95fc2ac1dbc 106
mbed_official 31:f95fc2ac1dbc 107 fs_store(&fs_config,
mbed_official 31:f95fc2ac1dbc 108 fs_config.p_start_addr,
mbed_official 31:f95fc2ac1dbc 109 reinterpret_cast<uint32_t *>(&persistentParams),
mbed_official 31:f95fc2ac1dbc 110 sizeof(PersistentParams_t) / 4);
mbed_official 3:5120491ba317 111 }
mbed_official 3:5120491ba317 112
mbed_official 3:5120491ba317 113 #endif /* #ifdef TARGET_NRF51822 */