File: MicroBitStorage.cpp

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (437 lines) | stat: -rw-r--r-- 13,921 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
The MIT License (MIT)

Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.

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.
*/

/**
  * Class definition for the MicroBitStorage class.
  * This allows reading and writing of FLASH memory.
  */

#include "MicroBitConfig.h"
#include "MicroBitStorage.h"
#include "MicroBitFlash.h"
#include "MicroBitCompat.h"


/**
  * Default constructor.
  *
  * Creates an instance of MicroBitStorage which acts like a KeyValueStore
  * that allows the retrieval, addition and deletion of KeyValuePairs.
  */
MicroBitStorage::MicroBitStorage()
{
    //initialise our magic block, if required.
    size();
}

/**
  * Method for erasing a page in flash.
  *
  * @param page_address Address of the first word in the page to be erased.
  */
void MicroBitStorage::flashPageErase(uint32_t * page_address)
{
    MicroBitFlash flash;
    flash.erase_page(page_address);
}

/**
  * Function for copying words from one location to another.
  *
  * @param from the address to copy data from.
  *
  * @param to the address to copy the data to.
  *
  * @param sizeInWords the number of words to copy
  */
void MicroBitStorage::flashCopy(uint32_t* from, uint32_t* to, int sizeInWords)
{
    MicroBitFlash flash;
    flash.flash_burn(to, from, sizeInWords);
}

/**
  * Method for writing a word of data in flash with a value.
  *
  * @param address Address of the word to change.
  *
  * @param value Value to be written to flash.
  */
void MicroBitStorage::flashWordWrite(uint32_t * address, uint32_t value)
{
    flashCopy(&value, address, 1);
}

/**
  * Function for populating the scratch page with a KeyValueStore.
  *
  * @param store the KeyValueStore struct to write to the scratch page.
  */
void MicroBitStorage::scratchKeyValueStore(KeyValueStore store)
{
    //calculate our various offsets
    uint32_t *s = (uint32_t *) &store;
    uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
    uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));
    int wordsToWrite = sizeof(KeyValueStore) / 4;

    flashCopy(s, scratchPointer, wordsToWrite);
}

/**
  * Function for populating the scratch page with a KeyValuePair.
  *
  * @param pair the KeyValuePair struct to write to the scratch page.
  *
  * @param flashPointer the pointer in flash where this KeyValuePair resides. This pointer
  * is used to determine the offset into the scratch page, where the KeyValuePair should
  * be written.
  */
void MicroBitStorage::scratchKeyValuePair(KeyValuePair pair, uint32_t* flashPointer)
{
    //we can only write using words
    uint32_t *p = (uint32_t *) &pair;

    //calculate our various offsets
    uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
    uint32_t pg_num  = NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET;

    uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));
    uint32_t *flashBlockPointer = (uint32_t *)(pg_size * pg_num);

    uint32_t flashPointerOffset = flashPointer - flashBlockPointer;

    scratchPointer += flashPointerOffset;

    //KeyValuePair is word aligned...
    int wordsToWrite = sizeof(KeyValuePair) / 4;

    flashCopy(p, scratchPointer, wordsToWrite);
}

/**
  * Places a given key, and it's corresponding value into flash at the earliest
  * available point.
  *
  * @param key the unique name that should be used as an identifier for the given data.
  *            The key is presumed to be null terminated.
  *
  * @param data a pointer to the beginning of the data to be persisted.
  *
  * @param dataSize the size of the data to be persisted
  *
  * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large,
  *         MICROBIT_NO_RESOURCES if the storage page is full
  */
int MicroBitStorage::put(const char *key, uint8_t *data, int dataSize)
{
    KeyValuePair pair = KeyValuePair();

    int keySize = strlen(key) + 1;

    if(keySize > (int)sizeof(pair.key) || dataSize > (int)sizeof(pair.value) || dataSize < 0)
        return MICROBIT_INVALID_PARAMETER;

    KeyValuePair *currentValue = get(key);

    int upToDate = currentValue && (memcmp(currentValue->value, data, dataSize) == 0);

    if(currentValue)
        delete currentValue;

    if(upToDate)
        return MICROBIT_OK;

    memcpy(pair.key, key, keySize);
    memcpy(pair.value, data, dataSize);

    //calculate our various offsets.
    uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
    uint32_t *flashPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET));
    uint32_t *flashBlockPointer = flashPointer;
    uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));

    uint32_t kvStoreSize = sizeof(KeyValueStore) / 4;
    uint32_t kvPairSize = sizeof(KeyValuePair) / 4;

    int storeSize = size();

    //our KeyValueStore struct is always at 0
    flashPointer += kvStoreSize;

    KeyValuePair storedPair = KeyValuePair();

    int found = 0;

    //erase our scratch page
    flashPageErase(scratchPointer);

    //iterate through key value pairs in flash, writing them to the scratch page.
    for(int i = 0; i < storeSize; i++)
    {
        memcpy(&storedPair, flashPointer, sizeof(KeyValuePair));

        //check if the keys match...
        if(strcmp((char *)storedPair.key, (char *)pair.key) == 0)
        {
            found = 1;
            //scratch our KeyValueStore struct so that it is preserved.
            scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize));
            scratchKeyValuePair(pair, flashPointer);
        }
        else
        {
            scratchKeyValuePair(storedPair, flashPointer);
        }

        flashPointer += kvPairSize;
    }

    if(!found)
    {
        //if we haven't got a match for the key, check we can add a new KeyValuePair
        if(storeSize == (int)((pg_size - kvStoreSize) / MICROBIT_STORAGE_BLOCK_SIZE))
            return MICROBIT_NO_RESOURCES;

        storeSize += 1;

        //scratch our updated values.
        scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize));
        scratchKeyValuePair(pair, flashPointer);
    }

    //erase our storage page
    flashPageErase((uint32_t *)flashBlockPointer);

    //copy from scratch to storage.
    flashCopy((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)), flashBlockPointer, kvStoreSize + (storeSize * kvPairSize));

    return MICROBIT_OK;
}

/**
  * Places a given key, and it's corresponding value into flash at the earliest
  * available point.
  *
  * @param key the unique name that should be used as an identifier for the given data.
  *
  * @param data a pointer to the beginning of the data to be persisted.
  *
  * @param dataSize the size of the data to be persisted
  *
  * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large,
  *         MICROBIT_NO_RESOURCES if the storage page is full
  */
int MicroBitStorage::put(ManagedString key, uint8_t* data, int dataSize)
{
    return put((char *)key.toCharArray(), data, dataSize);
}

/**
  * Retreives a KeyValuePair identified by a given key.
  *
  * @param key the unique name used to identify a KeyValuePair in flash.
  *
  * @return a pointer to a heap allocated KeyValuePair struct, this pointer will be
  *         NULL if the key was not found in storage.
  *
  * @note it is up to the user to free memory after use.
  */
KeyValuePair* MicroBitStorage::get(const char* key)
{
    //calculate our offsets for our storage page
    uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
    uint32_t pg_num  = NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET;

    uint32_t *flashBlockPointer = (uint32_t *)(pg_size * pg_num);

    int storeSize = size();

    //we haven't got anything stored, so return...
    if(storeSize == 0)
        return NULL;

    //our KeyValueStore struct is always at 0
    flashBlockPointer += sizeof(KeyValueStore) / 4;

    KeyValuePair *pair = new KeyValuePair();

    int i;

    //iterate through flash until we have a match, or drop out.
    for(i = 0; i < storeSize; i++)
    {
        memcpy(pair, flashBlockPointer, sizeof(KeyValuePair));

        if(strcmp(key,(char *)pair->key) == 0)
            break;

        flashBlockPointer += sizeof(KeyValuePair) / 4;
    }

    //clean up
    if(i == storeSize)
    {
        delete pair;
        return NULL;
    }

    return pair;
}

/**
  * Retreives a KeyValuePair identified by a given key.
  *
  * @param key the unique name used to identify a KeyValuePair in flash.
  *
  * @return a pointer to a heap allocated KeyValuePair struct, this pointer will be
  *         NULL if the key was not found in storage.
  *
  * @note it is up to the user to free memory after use.
  */
KeyValuePair* MicroBitStorage::get(ManagedString key)
{
    return get((char *)key.toCharArray());
}

/**
  * Removes a KeyValuePair identified by a given key.
  *
  * @param key the unique name used to identify a KeyValuePair in flash.
  *
  * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the given key
  *         was not found in flash.
  */
int MicroBitStorage::remove(const char* key)
{
    //calculate our various offsets
    uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
    uint32_t *flashPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET));
    uint32_t *flashBlockPointer = flashPointer;
    uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));

    uint32_t kvStoreSize = sizeof(KeyValueStore) / 4;
    uint32_t kvPairSize = sizeof(KeyValuePair) / 4;

    int storeSize = size();

    //if we have no data, we have nothing to do.
    if(storeSize == 0)
        return MICROBIT_NO_DATA;

    //our KeyValueStore struct is always at 0
    flashPointer += kvStoreSize;
    scratchPointer += kvStoreSize;

    KeyValuePair storedPair = KeyValuePair();

    int found = 0;

    //set up our scratch area
    flashPageErase(scratchPointer);

    //iterate through our flash copy pairs to scratch, unless there is a key patch
    for(int i = 0; i < storeSize; i++)
    {
        memcpy(&storedPair, flashPointer, sizeof(KeyValuePair));

        //if we have a match, don't increment our scratchPointer
        if(strcmp((char *)storedPair.key, (char *)key) == 0)
        {
            found = 1;
            //write our new KeyValueStore data
            scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize - 1));
        }
        else
        {
            //otherwise copy the KeyValuePair from our storage page.
            flashCopy(flashPointer, scratchPointer, sizeof(KeyValuePair) / 4);
            scratchPointer += sizeof(KeyValuePair) / 4;
        }

        flashPointer += sizeof(KeyValuePair) / 4;
    }

    //if we haven't got a match, write our old KeyValueStore struct
    if(!found)
    {
        scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize));
        return MICROBIT_NO_DATA;
    }

    //copy scratch to our storage page
    flashPageErase((uint32_t *)flashBlockPointer);
    flashCopy((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)), flashBlockPointer, kvStoreSize + (storeSize * kvPairSize));

    return MICROBIT_OK;
}

/**
  * Removes a KeyValuePair identified by a given key.
  *
  * @param key the unique name used to identify a KeyValuePair in flash.
  *
  * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the given key
  *         was not found in flash.
  */
int MicroBitStorage::remove(ManagedString key)
{
    return remove((char *)key.toCharArray());
}

/**
  * The size of the flash based KeyValueStore.
  *
  * @return the number of entries in the key value store
  */
int MicroBitStorage::size()
{
    uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
    uint32_t pg_num  = NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET;

    uint32_t *flashBlockPointer = (uint32_t *)(pg_size * pg_num);

    KeyValueStore store = KeyValueStore();

    //read our data!
    memcpy(&store, flashBlockPointer, sizeof(KeyValueStore));

    //if we haven't used flash before, we need to configure it
    if(store.magic != MICROBIT_STORAGE_MAGIC)
    {
        store.magic = MICROBIT_STORAGE_MAGIC;
        store.size = 0;

        //erase the scratch page and write our new KeyValueStore
        flashPageErase((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)));
        scratchKeyValueStore(store);

        //erase flash, and copy the scratch page over
        flashPageErase((uint32_t *)flashBlockPointer);
        flashCopy((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)), flashBlockPointer, pg_size/4);
    }

    return store.size;
}