Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /**
MrBedfordVan 0:b9164b348919 27 * Class definition for the MicroBitStorage class.
MrBedfordVan 0:b9164b348919 28 * This allows reading and writing of FLASH memory.
MrBedfordVan 0:b9164b348919 29 */
MrBedfordVan 0:b9164b348919 30
MrBedfordVan 0:b9164b348919 31 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 32 #include "MicroBitStorage.h"
MrBedfordVan 0:b9164b348919 33 #include "MicroBitCompat.h"
MrBedfordVan 0:b9164b348919 34
MrBedfordVan 0:b9164b348919 35 /**
MrBedfordVan 0:b9164b348919 36 * Default constructor.
MrBedfordVan 0:b9164b348919 37 *
MrBedfordVan 0:b9164b348919 38 * Creates an instance of MicroBitStorage which acts like a KeyValueStore
MrBedfordVan 0:b9164b348919 39 * that allows the retrieval, addition and deletion of KeyValuePairs.
MrBedfordVan 0:b9164b348919 40 */
MrBedfordVan 0:b9164b348919 41 MicroBitStorage::MicroBitStorage()
MrBedfordVan 0:b9164b348919 42 {
MrBedfordVan 0:b9164b348919 43 //initialise our magic block, if required.
MrBedfordVan 0:b9164b348919 44 size();
MrBedfordVan 0:b9164b348919 45 }
MrBedfordVan 0:b9164b348919 46
MrBedfordVan 0:b9164b348919 47 /**
MrBedfordVan 0:b9164b348919 48 * Writes the given number of bytes to the address specified.
MrBedfordVan 0:b9164b348919 49 *
MrBedfordVan 0:b9164b348919 50 * @param buffer the data to write.
MrBedfordVan 0:b9164b348919 51 *
MrBedfordVan 0:b9164b348919 52 * @param address the location in memory to write to.
MrBedfordVan 0:b9164b348919 53 *
MrBedfordVan 0:b9164b348919 54 * @param length the number of bytes to write.
MrBedfordVan 0:b9164b348919 55 *
MrBedfordVan 0:b9164b348919 56 * @note currently not implemented.
MrBedfordVan 0:b9164b348919 57 */
MrBedfordVan 0:b9164b348919 58 int MicroBitStorage::writeBytes(uint8_t *buffer, uint32_t address, int length)
MrBedfordVan 0:b9164b348919 59 {
MrBedfordVan 0:b9164b348919 60 (void) buffer;
MrBedfordVan 0:b9164b348919 61 (void) address;
MrBedfordVan 0:b9164b348919 62 (void) length;
MrBedfordVan 0:b9164b348919 63
MrBedfordVan 0:b9164b348919 64 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 65 }
MrBedfordVan 0:b9164b348919 66
MrBedfordVan 0:b9164b348919 67 /**
MrBedfordVan 0:b9164b348919 68 * Method for erasing a page in flash.
MrBedfordVan 0:b9164b348919 69 *
MrBedfordVan 0:b9164b348919 70 * @param page_address Address of the first word in the page to be erased.
MrBedfordVan 0:b9164b348919 71 */
MrBedfordVan 0:b9164b348919 72 void MicroBitStorage::flashPageErase(uint32_t * page_address)
MrBedfordVan 0:b9164b348919 73 {
MrBedfordVan 0:b9164b348919 74 // Turn on flash erase enable and wait until the NVMC is ready:
MrBedfordVan 0:b9164b348919 75 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
MrBedfordVan 0:b9164b348919 76
MrBedfordVan 0:b9164b348919 77 while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
MrBedfordVan 0:b9164b348919 78
MrBedfordVan 0:b9164b348919 79 // Erase page:
MrBedfordVan 0:b9164b348919 80 NRF_NVMC->ERASEPAGE = (uint32_t)page_address;
MrBedfordVan 0:b9164b348919 81
MrBedfordVan 0:b9164b348919 82 while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
MrBedfordVan 0:b9164b348919 83
MrBedfordVan 0:b9164b348919 84 // Turn off flash erase enable and wait until the NVMC is ready:
MrBedfordVan 0:b9164b348919 85 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
MrBedfordVan 0:b9164b348919 86
MrBedfordVan 0:b9164b348919 87 while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
MrBedfordVan 0:b9164b348919 88 }
MrBedfordVan 0:b9164b348919 89
MrBedfordVan 0:b9164b348919 90 /**
MrBedfordVan 0:b9164b348919 91 * Function for copying words from one location to another.
MrBedfordVan 0:b9164b348919 92 *
MrBedfordVan 0:b9164b348919 93 * @param from the address to copy data from.
MrBedfordVan 0:b9164b348919 94 *
MrBedfordVan 0:b9164b348919 95 * @param to the address to copy the data to.
MrBedfordVan 0:b9164b348919 96 *
MrBedfordVan 0:b9164b348919 97 * @param sizeInWords the number of words to copy
MrBedfordVan 0:b9164b348919 98 */
MrBedfordVan 0:b9164b348919 99 void MicroBitStorage::flashCopy(uint32_t* from, uint32_t* to, int sizeInWords)
MrBedfordVan 0:b9164b348919 100 {
MrBedfordVan 0:b9164b348919 101 // Turn on flash write enable and wait until the NVMC is ready:
MrBedfordVan 0:b9164b348919 102 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
MrBedfordVan 0:b9164b348919 103
MrBedfordVan 0:b9164b348919 104 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {};
MrBedfordVan 0:b9164b348919 105
MrBedfordVan 0:b9164b348919 106 for(int i = 0; i < sizeInWords; i++)
MrBedfordVan 0:b9164b348919 107 {
MrBedfordVan 0:b9164b348919 108 *(to + i) = *(from + i);
MrBedfordVan 0:b9164b348919 109 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {};
MrBedfordVan 0:b9164b348919 110 }
MrBedfordVan 0:b9164b348919 111
MrBedfordVan 0:b9164b348919 112 // Turn off flash write enable and wait until the NVMC is ready:
MrBedfordVan 0:b9164b348919 113 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
MrBedfordVan 0:b9164b348919 114 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {};
MrBedfordVan 0:b9164b348919 115 }
MrBedfordVan 0:b9164b348919 116
MrBedfordVan 0:b9164b348919 117 /**
MrBedfordVan 0:b9164b348919 118 * Method for writing a word of data in flash with a value.
MrBedfordVan 0:b9164b348919 119 *
MrBedfordVan 0:b9164b348919 120 * @param address Address of the word to change.
MrBedfordVan 0:b9164b348919 121 *
MrBedfordVan 0:b9164b348919 122 * @param value Value to be written to flash.
MrBedfordVan 0:b9164b348919 123 */
MrBedfordVan 0:b9164b348919 124 void MicroBitStorage::flashWordWrite(uint32_t * address, uint32_t value)
MrBedfordVan 0:b9164b348919 125 {
MrBedfordVan 0:b9164b348919 126 // Turn on flash write enable and wait until the NVMC is ready:
MrBedfordVan 0:b9164b348919 127 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
MrBedfordVan 0:b9164b348919 128
MrBedfordVan 0:b9164b348919 129 while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
MrBedfordVan 0:b9164b348919 130
MrBedfordVan 0:b9164b348919 131 *address = value;
MrBedfordVan 0:b9164b348919 132
MrBedfordVan 0:b9164b348919 133 while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
MrBedfordVan 0:b9164b348919 134
MrBedfordVan 0:b9164b348919 135 // Turn off flash write enable and wait until the NVMC is ready:
MrBedfordVan 0:b9164b348919 136 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
MrBedfordVan 0:b9164b348919 137
MrBedfordVan 0:b9164b348919 138 while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
MrBedfordVan 0:b9164b348919 139 }
MrBedfordVan 0:b9164b348919 140
MrBedfordVan 0:b9164b348919 141 /**
MrBedfordVan 0:b9164b348919 142 * Function for populating the scratch page with a KeyValueStore.
MrBedfordVan 0:b9164b348919 143 *
MrBedfordVan 0:b9164b348919 144 * @param store the KeyValueStore struct to write to the scratch page.
MrBedfordVan 0:b9164b348919 145 */
MrBedfordVan 0:b9164b348919 146 void MicroBitStorage::scratchKeyValueStore(KeyValueStore store)
MrBedfordVan 0:b9164b348919 147 {
MrBedfordVan 0:b9164b348919 148 //calculate our various offsets
MrBedfordVan 0:b9164b348919 149 uint32_t *s = (uint32_t *) &store;
MrBedfordVan 0:b9164b348919 150 uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
MrBedfordVan 0:b9164b348919 151
MrBedfordVan 0:b9164b348919 152 uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));
MrBedfordVan 0:b9164b348919 153
MrBedfordVan 0:b9164b348919 154 //KeyValueStore is word aligned.
MrBedfordVan 0:b9164b348919 155 int wordsToWrite = sizeof(KeyValueStore) / 4;
MrBedfordVan 0:b9164b348919 156
MrBedfordVan 0:b9164b348919 157 //write the given KeyValueStore
MrBedfordVan 0:b9164b348919 158 for (int i = 0; i < wordsToWrite; i++)
MrBedfordVan 0:b9164b348919 159 {
MrBedfordVan 0:b9164b348919 160 flashWordWrite(scratchPointer, *s);
MrBedfordVan 0:b9164b348919 161 scratchPointer++;
MrBedfordVan 0:b9164b348919 162 s++;
MrBedfordVan 0:b9164b348919 163 }
MrBedfordVan 0:b9164b348919 164 }
MrBedfordVan 0:b9164b348919 165
MrBedfordVan 0:b9164b348919 166 /**
MrBedfordVan 0:b9164b348919 167 * Function for populating the scratch page with a KeyValuePair.
MrBedfordVan 0:b9164b348919 168 *
MrBedfordVan 0:b9164b348919 169 * @param pair the KeyValuePair struct to write to the scratch page.
MrBedfordVan 0:b9164b348919 170 *
MrBedfordVan 0:b9164b348919 171 * @param flashPointer the pointer in flash where this KeyValuePair resides. This pointer
MrBedfordVan 0:b9164b348919 172 * is used to determine the offset into the scratch page, where the KeyValuePair should
MrBedfordVan 0:b9164b348919 173 * be written.
MrBedfordVan 0:b9164b348919 174 */
MrBedfordVan 0:b9164b348919 175 void MicroBitStorage::scratchKeyValuePair(KeyValuePair pair, uint32_t* flashPointer)
MrBedfordVan 0:b9164b348919 176 {
MrBedfordVan 0:b9164b348919 177 //we can only write using words
MrBedfordVan 0:b9164b348919 178 uint32_t *p = (uint32_t *) &pair;
MrBedfordVan 0:b9164b348919 179
MrBedfordVan 0:b9164b348919 180 //calculate our various offsets
MrBedfordVan 0:b9164b348919 181 uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
MrBedfordVan 0:b9164b348919 182 uint32_t pg_num = NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET;
MrBedfordVan 0:b9164b348919 183
MrBedfordVan 0:b9164b348919 184 uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));
MrBedfordVan 0:b9164b348919 185 uint32_t *flashBlockPointer = (uint32_t *)(pg_size * pg_num);
MrBedfordVan 0:b9164b348919 186
MrBedfordVan 0:b9164b348919 187 uint32_t flashPointerOffset = flashPointer - flashBlockPointer;
MrBedfordVan 0:b9164b348919 188
MrBedfordVan 0:b9164b348919 189 scratchPointer += flashPointerOffset;
MrBedfordVan 0:b9164b348919 190
MrBedfordVan 0:b9164b348919 191 //KeyValuePair is word aligned...
MrBedfordVan 0:b9164b348919 192 int wordsToWrite = sizeof(KeyValuePair) / 4;
MrBedfordVan 0:b9164b348919 193
MrBedfordVan 0:b9164b348919 194 //write
MrBedfordVan 0:b9164b348919 195 for (int i = 0; i < wordsToWrite; i++)
MrBedfordVan 0:b9164b348919 196 {
MrBedfordVan 0:b9164b348919 197 flashWordWrite(scratchPointer, *p);
MrBedfordVan 0:b9164b348919 198 scratchPointer++;
MrBedfordVan 0:b9164b348919 199 p++;
MrBedfordVan 0:b9164b348919 200 }
MrBedfordVan 0:b9164b348919 201 }
MrBedfordVan 0:b9164b348919 202
MrBedfordVan 0:b9164b348919 203 /**
MrBedfordVan 0:b9164b348919 204 * Places a given key, and it's corresponding value into flash at the earliest
MrBedfordVan 0:b9164b348919 205 * available point.
MrBedfordVan 0:b9164b348919 206 *
MrBedfordVan 0:b9164b348919 207 * @param key the unique name that should be used as an identifier for the given data.
MrBedfordVan 0:b9164b348919 208 * The key is presumed to be null terminated.
MrBedfordVan 0:b9164b348919 209 *
MrBedfordVan 0:b9164b348919 210 * @param data a pointer to the beginning of the data to be persisted.
MrBedfordVan 0:b9164b348919 211 *
MrBedfordVan 0:b9164b348919 212 * @param dataSize the size of the data to be persisted
MrBedfordVan 0:b9164b348919 213 *
MrBedfordVan 0:b9164b348919 214 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large,
MrBedfordVan 0:b9164b348919 215 * MICROBIT_NO_RESOURCES if the storage page is full
MrBedfordVan 0:b9164b348919 216 */
MrBedfordVan 0:b9164b348919 217 int MicroBitStorage::put(const char *key, uint8_t *data, int dataSize)
MrBedfordVan 0:b9164b348919 218 {
MrBedfordVan 0:b9164b348919 219 KeyValuePair pair = KeyValuePair();
MrBedfordVan 0:b9164b348919 220
MrBedfordVan 0:b9164b348919 221 int keySize = strlen(key) + 1;
MrBedfordVan 0:b9164b348919 222
MrBedfordVan 0:b9164b348919 223 if(keySize > (int)sizeof(pair.key) || dataSize > (int)sizeof(pair.value) || dataSize < 0)
MrBedfordVan 0:b9164b348919 224 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 225
MrBedfordVan 0:b9164b348919 226 KeyValuePair *currentValue = get(key);
MrBedfordVan 0:b9164b348919 227
MrBedfordVan 0:b9164b348919 228 int upToDate = currentValue && (memcmp(currentValue->value, data, dataSize) == 0);
MrBedfordVan 0:b9164b348919 229
MrBedfordVan 0:b9164b348919 230 if(currentValue)
MrBedfordVan 0:b9164b348919 231 delete currentValue;
MrBedfordVan 0:b9164b348919 232
MrBedfordVan 0:b9164b348919 233 if(upToDate)
MrBedfordVan 0:b9164b348919 234 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 235
MrBedfordVan 0:b9164b348919 236 memcpy(pair.key, key, keySize);
MrBedfordVan 0:b9164b348919 237 memcpy(pair.value, data, dataSize);
MrBedfordVan 0:b9164b348919 238
MrBedfordVan 0:b9164b348919 239 //calculate our various offsets.
MrBedfordVan 0:b9164b348919 240 uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
MrBedfordVan 0:b9164b348919 241 uint32_t *flashPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET));
MrBedfordVan 0:b9164b348919 242 uint32_t *flashBlockPointer = flashPointer;
MrBedfordVan 0:b9164b348919 243 uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));
MrBedfordVan 0:b9164b348919 244
MrBedfordVan 0:b9164b348919 245 uint32_t kvStoreSize = sizeof(KeyValueStore) / 4;
MrBedfordVan 0:b9164b348919 246 uint32_t kvPairSize = sizeof(KeyValuePair) / 4;
MrBedfordVan 0:b9164b348919 247
MrBedfordVan 0:b9164b348919 248 int storeSize = size();
MrBedfordVan 0:b9164b348919 249
MrBedfordVan 0:b9164b348919 250 //our KeyValueStore struct is always at 0
MrBedfordVan 0:b9164b348919 251 flashPointer += kvStoreSize;
MrBedfordVan 0:b9164b348919 252
MrBedfordVan 0:b9164b348919 253 KeyValuePair storedPair = KeyValuePair();
MrBedfordVan 0:b9164b348919 254
MrBedfordVan 0:b9164b348919 255 int found = 0;
MrBedfordVan 0:b9164b348919 256
MrBedfordVan 0:b9164b348919 257 //erase our scratch page
MrBedfordVan 0:b9164b348919 258 flashPageErase(scratchPointer);
MrBedfordVan 0:b9164b348919 259
MrBedfordVan 0:b9164b348919 260 //iterate through key value pairs in flash, writing them to the scratch page.
MrBedfordVan 0:b9164b348919 261 for(int i = 0; i < storeSize; i++)
MrBedfordVan 0:b9164b348919 262 {
MrBedfordVan 0:b9164b348919 263 memcpy(&storedPair, flashPointer, sizeof(KeyValuePair));
MrBedfordVan 0:b9164b348919 264
MrBedfordVan 0:b9164b348919 265 //check if the keys match...
MrBedfordVan 0:b9164b348919 266 if(strcmp((char *)storedPair.key, (char *)pair.key) == 0)
MrBedfordVan 0:b9164b348919 267 {
MrBedfordVan 0:b9164b348919 268 found = 1;
MrBedfordVan 0:b9164b348919 269 //scratch our KeyValueStore struct so that it is preserved.
MrBedfordVan 0:b9164b348919 270 scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize));
MrBedfordVan 0:b9164b348919 271 scratchKeyValuePair(pair, flashPointer);
MrBedfordVan 0:b9164b348919 272 }
MrBedfordVan 0:b9164b348919 273 else
MrBedfordVan 0:b9164b348919 274 {
MrBedfordVan 0:b9164b348919 275 scratchKeyValuePair(storedPair, flashPointer);
MrBedfordVan 0:b9164b348919 276 }
MrBedfordVan 0:b9164b348919 277
MrBedfordVan 0:b9164b348919 278 flashPointer += kvPairSize;
MrBedfordVan 0:b9164b348919 279 }
MrBedfordVan 0:b9164b348919 280
MrBedfordVan 0:b9164b348919 281 if(!found)
MrBedfordVan 0:b9164b348919 282 {
MrBedfordVan 0:b9164b348919 283 //if we haven't got a match for the key, check we can add a new KeyValuePair
MrBedfordVan 0:b9164b348919 284 if(storeSize == (int)((pg_size - kvStoreSize) / MICROBIT_STORAGE_BLOCK_SIZE))
MrBedfordVan 0:b9164b348919 285 return MICROBIT_NO_RESOURCES;
MrBedfordVan 0:b9164b348919 286
MrBedfordVan 0:b9164b348919 287 storeSize += 1;
MrBedfordVan 0:b9164b348919 288
MrBedfordVan 0:b9164b348919 289 //scratch our updated values.
MrBedfordVan 0:b9164b348919 290 scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize));
MrBedfordVan 0:b9164b348919 291 scratchKeyValuePair(pair, flashPointer);
MrBedfordVan 0:b9164b348919 292 }
MrBedfordVan 0:b9164b348919 293
MrBedfordVan 0:b9164b348919 294 //erase our storage page
MrBedfordVan 0:b9164b348919 295 flashPageErase((uint32_t *)flashBlockPointer);
MrBedfordVan 0:b9164b348919 296
MrBedfordVan 0:b9164b348919 297 //copy from scratch to storage.
MrBedfordVan 0:b9164b348919 298 flashCopy((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)), flashBlockPointer, kvStoreSize + (storeSize * kvPairSize));
MrBedfordVan 0:b9164b348919 299
MrBedfordVan 0:b9164b348919 300 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 301 }
MrBedfordVan 0:b9164b348919 302
MrBedfordVan 0:b9164b348919 303 /**
MrBedfordVan 0:b9164b348919 304 * Places a given key, and it's corresponding value into flash at the earliest
MrBedfordVan 0:b9164b348919 305 * available point.
MrBedfordVan 0:b9164b348919 306 *
MrBedfordVan 0:b9164b348919 307 * @param key the unique name that should be used as an identifier for the given data.
MrBedfordVan 0:b9164b348919 308 *
MrBedfordVan 0:b9164b348919 309 * @param data a pointer to the beginning of the data to be persisted.
MrBedfordVan 0:b9164b348919 310 *
MrBedfordVan 0:b9164b348919 311 * @param dataSize the size of the data to be persisted
MrBedfordVan 0:b9164b348919 312 *
MrBedfordVan 0:b9164b348919 313 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large,
MrBedfordVan 0:b9164b348919 314 * MICROBIT_NO_RESOURCES if the storage page is full
MrBedfordVan 0:b9164b348919 315 */
MrBedfordVan 0:b9164b348919 316 int MicroBitStorage::put(ManagedString key, uint8_t* data, int dataSize)
MrBedfordVan 0:b9164b348919 317 {
MrBedfordVan 0:b9164b348919 318 return put((char *)key.toCharArray(), data, dataSize);
MrBedfordVan 0:b9164b348919 319 }
MrBedfordVan 0:b9164b348919 320
MrBedfordVan 0:b9164b348919 321 /**
MrBedfordVan 0:b9164b348919 322 * Retreives a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 323 *
MrBedfordVan 0:b9164b348919 324 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 325 *
MrBedfordVan 0:b9164b348919 326 * @return a pointer to a heap allocated KeyValuePair struct, this pointer will be
MrBedfordVan 0:b9164b348919 327 * NULL if the key was not found in storage.
MrBedfordVan 0:b9164b348919 328 *
MrBedfordVan 0:b9164b348919 329 * @note it is up to the user to free memory after use.
MrBedfordVan 0:b9164b348919 330 */
MrBedfordVan 0:b9164b348919 331 KeyValuePair* MicroBitStorage::get(const char* key)
MrBedfordVan 0:b9164b348919 332 {
MrBedfordVan 0:b9164b348919 333 //calculate our offsets for our storage page
MrBedfordVan 0:b9164b348919 334 uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
MrBedfordVan 0:b9164b348919 335 uint32_t pg_num = NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET;
MrBedfordVan 0:b9164b348919 336
MrBedfordVan 0:b9164b348919 337 uint32_t *flashBlockPointer = (uint32_t *)(pg_size * pg_num);
MrBedfordVan 0:b9164b348919 338
MrBedfordVan 0:b9164b348919 339 int storeSize = size();
MrBedfordVan 0:b9164b348919 340
MrBedfordVan 0:b9164b348919 341 //we haven't got anything stored, so return...
MrBedfordVan 0:b9164b348919 342 if(storeSize == 0)
MrBedfordVan 0:b9164b348919 343 return NULL;
MrBedfordVan 0:b9164b348919 344
MrBedfordVan 0:b9164b348919 345 //our KeyValueStore struct is always at 0
MrBedfordVan 0:b9164b348919 346 flashBlockPointer += sizeof(KeyValueStore) / 4;
MrBedfordVan 0:b9164b348919 347
MrBedfordVan 0:b9164b348919 348 KeyValuePair *pair = new KeyValuePair();
MrBedfordVan 0:b9164b348919 349
MrBedfordVan 0:b9164b348919 350 int i;
MrBedfordVan 0:b9164b348919 351
MrBedfordVan 0:b9164b348919 352 //iterate through flash until we have a match, or drop out.
MrBedfordVan 0:b9164b348919 353 for(i = 0; i < storeSize; i++)
MrBedfordVan 0:b9164b348919 354 {
MrBedfordVan 0:b9164b348919 355 memcpy(pair, flashBlockPointer, sizeof(KeyValuePair));
MrBedfordVan 0:b9164b348919 356
MrBedfordVan 0:b9164b348919 357 if(strcmp(key,(char *)pair->key) == 0)
MrBedfordVan 0:b9164b348919 358 break;
MrBedfordVan 0:b9164b348919 359
MrBedfordVan 0:b9164b348919 360 flashBlockPointer += sizeof(KeyValuePair) / 4;
MrBedfordVan 0:b9164b348919 361 }
MrBedfordVan 0:b9164b348919 362
MrBedfordVan 0:b9164b348919 363 //clean up
MrBedfordVan 0:b9164b348919 364 if(i == storeSize)
MrBedfordVan 0:b9164b348919 365 {
MrBedfordVan 0:b9164b348919 366 delete pair;
MrBedfordVan 0:b9164b348919 367 return NULL;
MrBedfordVan 0:b9164b348919 368 }
MrBedfordVan 0:b9164b348919 369
MrBedfordVan 0:b9164b348919 370 return pair;
MrBedfordVan 0:b9164b348919 371 }
MrBedfordVan 0:b9164b348919 372
MrBedfordVan 0:b9164b348919 373 /**
MrBedfordVan 0:b9164b348919 374 * Retreives a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 375 *
MrBedfordVan 0:b9164b348919 376 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 377 *
MrBedfordVan 0:b9164b348919 378 * @return a pointer to a heap allocated KeyValuePair struct, this pointer will be
MrBedfordVan 0:b9164b348919 379 * NULL if the key was not found in storage.
MrBedfordVan 0:b9164b348919 380 *
MrBedfordVan 0:b9164b348919 381 * @note it is up to the user to free memory after use.
MrBedfordVan 0:b9164b348919 382 */
MrBedfordVan 0:b9164b348919 383 KeyValuePair* MicroBitStorage::get(ManagedString key)
MrBedfordVan 0:b9164b348919 384 {
MrBedfordVan 0:b9164b348919 385 return get((char *)key.toCharArray());
MrBedfordVan 0:b9164b348919 386 }
MrBedfordVan 0:b9164b348919 387
MrBedfordVan 0:b9164b348919 388 /**
MrBedfordVan 0:b9164b348919 389 * Removes a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 390 *
MrBedfordVan 0:b9164b348919 391 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 392 *
MrBedfordVan 0:b9164b348919 393 * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the given key
MrBedfordVan 0:b9164b348919 394 * was not found in flash.
MrBedfordVan 0:b9164b348919 395 */
MrBedfordVan 0:b9164b348919 396 int MicroBitStorage::remove(const char* key)
MrBedfordVan 0:b9164b348919 397 {
MrBedfordVan 0:b9164b348919 398 //calculate our various offsets
MrBedfordVan 0:b9164b348919 399 uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
MrBedfordVan 0:b9164b348919 400 uint32_t *flashPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET));
MrBedfordVan 0:b9164b348919 401 uint32_t *flashBlockPointer = flashPointer;
MrBedfordVan 0:b9164b348919 402 uint32_t *scratchPointer = (uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET));
MrBedfordVan 0:b9164b348919 403
MrBedfordVan 0:b9164b348919 404 uint32_t kvStoreSize = sizeof(KeyValueStore) / 4;
MrBedfordVan 0:b9164b348919 405 uint32_t kvPairSize = sizeof(KeyValuePair) / 4;
MrBedfordVan 0:b9164b348919 406
MrBedfordVan 0:b9164b348919 407 int storeSize = size();
MrBedfordVan 0:b9164b348919 408
MrBedfordVan 0:b9164b348919 409 //if we have no data, we have nothing to do.
MrBedfordVan 0:b9164b348919 410 if(storeSize == 0)
MrBedfordVan 0:b9164b348919 411 return MICROBIT_NO_DATA;
MrBedfordVan 0:b9164b348919 412
MrBedfordVan 0:b9164b348919 413 //our KeyValueStore struct is always at 0
MrBedfordVan 0:b9164b348919 414 flashPointer += kvStoreSize;
MrBedfordVan 0:b9164b348919 415 scratchPointer += kvStoreSize;
MrBedfordVan 0:b9164b348919 416
MrBedfordVan 0:b9164b348919 417 KeyValuePair storedPair = KeyValuePair();
MrBedfordVan 0:b9164b348919 418
MrBedfordVan 0:b9164b348919 419 int found = 0;
MrBedfordVan 0:b9164b348919 420
MrBedfordVan 0:b9164b348919 421 //set up our scratch area
MrBedfordVan 0:b9164b348919 422 flashPageErase(scratchPointer);
MrBedfordVan 0:b9164b348919 423
MrBedfordVan 0:b9164b348919 424 //iterate through our flash copy pairs to scratch, unless there is a key patch
MrBedfordVan 0:b9164b348919 425 for(int i = 0; i < storeSize; i++)
MrBedfordVan 0:b9164b348919 426 {
MrBedfordVan 0:b9164b348919 427 memcpy(&storedPair, flashPointer, sizeof(KeyValuePair));
MrBedfordVan 0:b9164b348919 428
MrBedfordVan 0:b9164b348919 429 //if we have a match, don't increment our scratchPointer
MrBedfordVan 0:b9164b348919 430 if(strcmp((char *)storedPair.key, (char *)key) == 0)
MrBedfordVan 0:b9164b348919 431 {
MrBedfordVan 0:b9164b348919 432 found = 1;
MrBedfordVan 0:b9164b348919 433 //write our new KeyValueStore data
MrBedfordVan 0:b9164b348919 434 scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize - 1));
MrBedfordVan 0:b9164b348919 435 }
MrBedfordVan 0:b9164b348919 436 else
MrBedfordVan 0:b9164b348919 437 {
MrBedfordVan 0:b9164b348919 438 //otherwise copy the KeyValuePair from our storage page.
MrBedfordVan 0:b9164b348919 439 flashCopy(flashPointer, scratchPointer, sizeof(KeyValuePair) / 4);
MrBedfordVan 0:b9164b348919 440 scratchPointer += sizeof(KeyValuePair) / 4;
MrBedfordVan 0:b9164b348919 441 }
MrBedfordVan 0:b9164b348919 442
MrBedfordVan 0:b9164b348919 443 flashPointer += sizeof(KeyValuePair) / 4;
MrBedfordVan 0:b9164b348919 444 }
MrBedfordVan 0:b9164b348919 445
MrBedfordVan 0:b9164b348919 446 //if we haven't got a match, write our old KeyValueStore struct
MrBedfordVan 0:b9164b348919 447 if(!found)
MrBedfordVan 0:b9164b348919 448 {
MrBedfordVan 0:b9164b348919 449 scratchKeyValueStore(KeyValueStore(MICROBIT_STORAGE_MAGIC, storeSize));
MrBedfordVan 0:b9164b348919 450 return MICROBIT_NO_DATA;
MrBedfordVan 0:b9164b348919 451 }
MrBedfordVan 0:b9164b348919 452
MrBedfordVan 0:b9164b348919 453 //copy scratch to our storage page
MrBedfordVan 0:b9164b348919 454 flashPageErase((uint32_t *)flashBlockPointer);
MrBedfordVan 0:b9164b348919 455 flashCopy((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)), flashBlockPointer, kvStoreSize + (storeSize * kvPairSize));
MrBedfordVan 0:b9164b348919 456
MrBedfordVan 0:b9164b348919 457 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 458 }
MrBedfordVan 0:b9164b348919 459
MrBedfordVan 0:b9164b348919 460 /**
MrBedfordVan 0:b9164b348919 461 * Removes a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 462 *
MrBedfordVan 0:b9164b348919 463 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 464 *
MrBedfordVan 0:b9164b348919 465 * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the given key
MrBedfordVan 0:b9164b348919 466 * was not found in flash.
MrBedfordVan 0:b9164b348919 467 */
MrBedfordVan 0:b9164b348919 468 int MicroBitStorage::remove(ManagedString key)
MrBedfordVan 0:b9164b348919 469 {
MrBedfordVan 0:b9164b348919 470 return remove((char *)key.toCharArray());
MrBedfordVan 0:b9164b348919 471 }
MrBedfordVan 0:b9164b348919 472
MrBedfordVan 0:b9164b348919 473 /**
MrBedfordVan 0:b9164b348919 474 * The size of the flash based KeyValueStore.
MrBedfordVan 0:b9164b348919 475 *
MrBedfordVan 0:b9164b348919 476 * @return the number of entries in the key value store
MrBedfordVan 0:b9164b348919 477 */
MrBedfordVan 0:b9164b348919 478 int MicroBitStorage::size()
MrBedfordVan 0:b9164b348919 479 {
MrBedfordVan 0:b9164b348919 480 uint32_t pg_size = NRF_FICR->CODEPAGESIZE;
MrBedfordVan 0:b9164b348919 481 uint32_t pg_num = NRF_FICR->CODESIZE - MICROBIT_STORAGE_STORE_PAGE_OFFSET;
MrBedfordVan 0:b9164b348919 482
MrBedfordVan 0:b9164b348919 483 uint32_t *flashBlockPointer = (uint32_t *)(pg_size * pg_num);
MrBedfordVan 0:b9164b348919 484
MrBedfordVan 0:b9164b348919 485 KeyValueStore store = KeyValueStore();
MrBedfordVan 0:b9164b348919 486
MrBedfordVan 0:b9164b348919 487 //read our data!
MrBedfordVan 0:b9164b348919 488 memcpy(&store, flashBlockPointer, sizeof(KeyValueStore));
MrBedfordVan 0:b9164b348919 489
MrBedfordVan 0:b9164b348919 490 //if we haven't used flash before, we need to configure it
MrBedfordVan 0:b9164b348919 491 if(store.magic != MICROBIT_STORAGE_MAGIC)
MrBedfordVan 0:b9164b348919 492 {
MrBedfordVan 0:b9164b348919 493 store.magic = MICROBIT_STORAGE_MAGIC;
MrBedfordVan 0:b9164b348919 494 store.size = 0;
MrBedfordVan 0:b9164b348919 495
MrBedfordVan 0:b9164b348919 496 //erase the scratch page and write our new KeyValueStore
MrBedfordVan 0:b9164b348919 497 flashPageErase((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)));
MrBedfordVan 0:b9164b348919 498 scratchKeyValueStore(store);
MrBedfordVan 0:b9164b348919 499
MrBedfordVan 0:b9164b348919 500 //erase flash, and copy the scratch page over
MrBedfordVan 0:b9164b348919 501 flashPageErase((uint32_t *)flashBlockPointer);
MrBedfordVan 0:b9164b348919 502 flashCopy((uint32_t *)(pg_size * (NRF_FICR->CODESIZE - MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET)), flashBlockPointer, pg_size/4);
MrBedfordVan 0:b9164b348919 503 }
MrBedfordVan 0:b9164b348919 504
MrBedfordVan 0:b9164b348919 505 return store.size;
MrBedfordVan 0:b9164b348919 506 }