Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOB: Full erase of data logging storage on first run. #79

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions source/samples/OOB_v3.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*
The MIT License (MIT)

Copyright (c) 2023 Micro:bit Educational Foundation
Copyright (c) 2023 Lancaster University

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "MicroBit.h"
#include "Synthesizer.h"
#include "StreamRecording.h"
Expand Down Expand Up @@ -302,6 +326,25 @@ static void onStart() {
uBit.display.print(HEART);
}

static void clearDataLoggingStorage() {
// This 32-bit word will be initially stored in flash as 0xffffffff
// On first run, it will be cleared to 0x00000000 in flash, to be able to
// use it as a flag to only erase the data logging storage once
static const uint32_t flash_original_value __ALIGNED(4) = 0xffffffff;
uint32_t zero = 0x00000000;

NRF52FlashManager flashManager(0, 128, 4096);

// Read the value currently in flash
uint32_t flash_current_value;
flashManager.read(&flash_current_value, (uint32_t)&flash_original_value, 1);
if (flash_current_value) {
// Value is not cleared yet, so full erase data storage and clear the flag
uBit.log.clear(true);
flashManager.write((uint32_t)&flash_original_value, &zero, 1);
}
}

void out_of_box_experience() {
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
Expand All @@ -312,8 +355,11 @@ void out_of_box_experience() {

onStart();

//uBit.audio.rawSplitter->status |= DEVICE_COMPONENT_STATUS_SYSTEM_TICK;
//uBit.audio.splitter->status |= DEVICE_COMPONENT_STATUS_SYSTEM_TICK;
// The Out of Box Experience is the default programme loaded to micro:bit from factory.
// It can be flashed to a micro:bit to resemble a "factory reset"
// For that, the data logging storage should be fully erased (by default it will do a quick erase)
// This process can take several seconds, so it is done in a separate fiber
create_fiber(clearDataLoggingStorage);

while (true) {
uBit.sleep(1000);
Expand Down
Loading