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 #ifndef MICROBIT_STORAGE_H
MrBedfordVan 0:b9164b348919 27 #define MICROBIT_STORAGE_H
MrBedfordVan 0:b9164b348919 28
MrBedfordVan 0:b9164b348919 29 #include "mbed.h"
MrBedfordVan 0:b9164b348919 30 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 31 #include "ManagedString.h"
MrBedfordVan 0:b9164b348919 32 #include "ErrorNo.h"
MrBedfordVan 0:b9164b348919 33
MrBedfordVan 0:b9164b348919 34 #define MICROBIT_STORAGE_MAGIC 0xCAFE
MrBedfordVan 0:b9164b348919 35
MrBedfordVan 0:b9164b348919 36 #define MICROBIT_STORAGE_BLOCK_SIZE 48
MrBedfordVan 0:b9164b348919 37 #define MICROBIT_STORAGE_KEY_SIZE 16
MrBedfordVan 0:b9164b348919 38 #define MICROBIT_STORAGE_VALUE_SIZE MICROBIT_STORAGE_BLOCK_SIZE - MICROBIT_STORAGE_KEY_SIZE
MrBedfordVan 0:b9164b348919 39
MrBedfordVan 0:b9164b348919 40 #define MICROBIT_STORAGE_STORE_PAGE_OFFSET 17 //Use the page just above the BLE Bond Data.
MrBedfordVan 0:b9164b348919 41 #define MICROBIT_STORAGE_SCRATCH_PAGE_OFFSET 19 //Use the page just below the BLE Bond Data.
MrBedfordVan 0:b9164b348919 42
MrBedfordVan 0:b9164b348919 43 struct KeyValuePair
MrBedfordVan 0:b9164b348919 44 {
MrBedfordVan 0:b9164b348919 45 uint8_t key[MICROBIT_STORAGE_KEY_SIZE];
MrBedfordVan 0:b9164b348919 46 uint8_t value[MICROBIT_STORAGE_VALUE_SIZE];
MrBedfordVan 0:b9164b348919 47 };
MrBedfordVan 0:b9164b348919 48
MrBedfordVan 0:b9164b348919 49 struct KeyValueStore
MrBedfordVan 0:b9164b348919 50 {
MrBedfordVan 0:b9164b348919 51 uint32_t magic;
MrBedfordVan 0:b9164b348919 52 uint32_t size;
MrBedfordVan 0:b9164b348919 53
MrBedfordVan 0:b9164b348919 54 KeyValueStore(uint32_t magic, uint32_t size)
MrBedfordVan 0:b9164b348919 55 {
MrBedfordVan 0:b9164b348919 56 this->magic = magic;
MrBedfordVan 0:b9164b348919 57 this->size = size;
MrBedfordVan 0:b9164b348919 58 }
MrBedfordVan 0:b9164b348919 59
MrBedfordVan 0:b9164b348919 60 KeyValueStore()
MrBedfordVan 0:b9164b348919 61 {
MrBedfordVan 0:b9164b348919 62 this->magic = 0;
MrBedfordVan 0:b9164b348919 63 this->size = 0;
MrBedfordVan 0:b9164b348919 64 }
MrBedfordVan 0:b9164b348919 65 };
MrBedfordVan 0:b9164b348919 66
MrBedfordVan 0:b9164b348919 67
MrBedfordVan 0:b9164b348919 68 /**
MrBedfordVan 0:b9164b348919 69 * Class definition for the MicroBitStorage class.
MrBedfordVan 0:b9164b348919 70 * This allows reading and writing of small blocks of data to FLASH memory.
MrBedfordVan 0:b9164b348919 71 *
MrBedfordVan 0:b9164b348919 72 * This class operates as a key value store, it allows the retrieval, addition
MrBedfordVan 0:b9164b348919 73 * and deletion of KeyValuePairs.
MrBedfordVan 0:b9164b348919 74 *
MrBedfordVan 0:b9164b348919 75 * The first 8 bytes are reserved for the KeyValueStore struct which gives core
MrBedfordVan 0:b9164b348919 76 * information such as the number of KeyValuePairs in the store, and whether the
MrBedfordVan 0:b9164b348919 77 * store has been initialised.
MrBedfordVan 0:b9164b348919 78 *
MrBedfordVan 0:b9164b348919 79 * After the KeyValueStore struct, KeyValuePairs are arranged contiguously until
MrBedfordVan 0:b9164b348919 80 * the end of the block used as persistent storage.
MrBedfordVan 0:b9164b348919 81 *
MrBedfordVan 0:b9164b348919 82 * |-------8-------|--------48-------|-----|---------48--------|
MrBedfordVan 0:b9164b348919 83 * | KeyValueStore | KeyValuePair[0] | ... | KeyValuePair[N-1] |
MrBedfordVan 0:b9164b348919 84 * |---------------|-----------------|-----|-------------------|
MrBedfordVan 0:b9164b348919 85 */
MrBedfordVan 0:b9164b348919 86 class MicroBitStorage
MrBedfordVan 0:b9164b348919 87 {
MrBedfordVan 0:b9164b348919 88 /**
MrBedfordVan 0:b9164b348919 89 * Function for copying words from one location to another.
MrBedfordVan 0:b9164b348919 90 *
MrBedfordVan 0:b9164b348919 91 * @param from the address to copy data from.
MrBedfordVan 0:b9164b348919 92 *
MrBedfordVan 0:b9164b348919 93 * @param to the address to copy the data to.
MrBedfordVan 0:b9164b348919 94 *
MrBedfordVan 0:b9164b348919 95 * @param sizeInWords the number of words to copy
MrBedfordVan 0:b9164b348919 96 */
MrBedfordVan 0:b9164b348919 97 void flashCopy(uint32_t* from, uint32_t* to, int sizeInWords);
MrBedfordVan 0:b9164b348919 98
MrBedfordVan 0:b9164b348919 99 /**
MrBedfordVan 0:b9164b348919 100 * Function for populating the scratch page with a KeyValueStore.
MrBedfordVan 0:b9164b348919 101 *
MrBedfordVan 0:b9164b348919 102 * @param store the KeyValueStore struct to write to the scratch page.
MrBedfordVan 0:b9164b348919 103 */
MrBedfordVan 0:b9164b348919 104 void scratchKeyValueStore(KeyValueStore store);
MrBedfordVan 0:b9164b348919 105
MrBedfordVan 0:b9164b348919 106 /**
MrBedfordVan 0:b9164b348919 107 * Function for populating the scratch page with a KeyValuePair.
MrBedfordVan 0:b9164b348919 108 *
MrBedfordVan 0:b9164b348919 109 * @param pair the KeyValuePair struct to write to the scratch page.
MrBedfordVan 0:b9164b348919 110 *
MrBedfordVan 0:b9164b348919 111 * @param flashPointer the pointer in flash where this KeyValuePair resides. This pointer
MrBedfordVan 0:b9164b348919 112 * is used to determine the offset into the scratch page, where the KeyValuePair should
MrBedfordVan 0:b9164b348919 113 * be written.
MrBedfordVan 0:b9164b348919 114 */
MrBedfordVan 0:b9164b348919 115 void scratchKeyValuePair(KeyValuePair pair, uint32_t* flashPointer);
MrBedfordVan 0:b9164b348919 116
MrBedfordVan 0:b9164b348919 117 public:
MrBedfordVan 0:b9164b348919 118
MrBedfordVan 0:b9164b348919 119 /**
MrBedfordVan 0:b9164b348919 120 * Default constructor.
MrBedfordVan 0:b9164b348919 121 *
MrBedfordVan 0:b9164b348919 122 * Creates an instance of MicroBitStorage which acts like a KeyValueStore
MrBedfordVan 0:b9164b348919 123 * that allows the retrieval, addition and deletion of KeyValuePairs.
MrBedfordVan 0:b9164b348919 124 */
MrBedfordVan 0:b9164b348919 125 MicroBitStorage();
MrBedfordVan 0:b9164b348919 126
MrBedfordVan 0:b9164b348919 127 /**
MrBedfordVan 0:b9164b348919 128 * Writes the given number of bytes to the address specified.
MrBedfordVan 0:b9164b348919 129 *
MrBedfordVan 0:b9164b348919 130 * @param buffer the data to write.
MrBedfordVan 0:b9164b348919 131 *
MrBedfordVan 0:b9164b348919 132 * @param address the location in memory to write to.
MrBedfordVan 0:b9164b348919 133 *
MrBedfordVan 0:b9164b348919 134 * @param length the number of bytes to write.
MrBedfordVan 0:b9164b348919 135 *
MrBedfordVan 0:b9164b348919 136 * @note currently not implemented.
MrBedfordVan 0:b9164b348919 137 */
MrBedfordVan 0:b9164b348919 138 int writeBytes(uint8_t *buffer, uint32_t address, int length);
MrBedfordVan 0:b9164b348919 139
MrBedfordVan 0:b9164b348919 140 /**
MrBedfordVan 0:b9164b348919 141 * Method for erasing a page in flash.
MrBedfordVan 0:b9164b348919 142 *
MrBedfordVan 0:b9164b348919 143 * @param page_address Address of the first word in the page to be erased.
MrBedfordVan 0:b9164b348919 144 */
MrBedfordVan 0:b9164b348919 145 void flashPageErase(uint32_t * page_address);
MrBedfordVan 0:b9164b348919 146
MrBedfordVan 0:b9164b348919 147 /**
MrBedfordVan 0:b9164b348919 148 * Method for writing a word of data in flash with a value.
MrBedfordVan 0:b9164b348919 149 *
MrBedfordVan 0:b9164b348919 150 * @param address Address of the word to change.
MrBedfordVan 0:b9164b348919 151 *
MrBedfordVan 0:b9164b348919 152 * @param value Value to be written to flash.
MrBedfordVan 0:b9164b348919 153 */
MrBedfordVan 0:b9164b348919 154 void flashWordWrite(uint32_t * address, uint32_t value);
MrBedfordVan 0:b9164b348919 155
MrBedfordVan 0:b9164b348919 156 /**
MrBedfordVan 0:b9164b348919 157 * Places a given key, and it's corresponding value into flash at the earliest
MrBedfordVan 0:b9164b348919 158 * available point.
MrBedfordVan 0:b9164b348919 159 *
MrBedfordVan 0:b9164b348919 160 * @param key the unique name that should be used as an identifier for the given data.
MrBedfordVan 0:b9164b348919 161 * The key is presumed to be null terminated.
MrBedfordVan 0:b9164b348919 162 *
MrBedfordVan 0:b9164b348919 163 * @param data a pointer to the beginning of the data to be persisted.
MrBedfordVan 0:b9164b348919 164 *
MrBedfordVan 0:b9164b348919 165 * @param dataSize the size of the data to be persisted
MrBedfordVan 0:b9164b348919 166 *
MrBedfordVan 0:b9164b348919 167 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large,
MrBedfordVan 0:b9164b348919 168 * MICROBIT_NO_RESOURCES if the storage page is full
MrBedfordVan 0:b9164b348919 169 */
MrBedfordVan 0:b9164b348919 170 int put(const char* key, uint8_t* data, int dataSize);
MrBedfordVan 0:b9164b348919 171
MrBedfordVan 0:b9164b348919 172
MrBedfordVan 0:b9164b348919 173 /**
MrBedfordVan 0:b9164b348919 174 * Places a given key, and it's corresponding value into flash at the earliest
MrBedfordVan 0:b9164b348919 175 * available point.
MrBedfordVan 0:b9164b348919 176 *
MrBedfordVan 0:b9164b348919 177 * @param key the unique name that should be used as an identifier for the given data.
MrBedfordVan 0:b9164b348919 178 *
MrBedfordVan 0:b9164b348919 179 * @param data a pointer to the beginning of the data to be persisted.
MrBedfordVan 0:b9164b348919 180 *
MrBedfordVan 0:b9164b348919 181 * @param dataSize the size of the data to be persisted
MrBedfordVan 0:b9164b348919 182 *
MrBedfordVan 0:b9164b348919 183 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if the key or size is too large,
MrBedfordVan 0:b9164b348919 184 * MICROBIT_NO_RESOURCES if the storage page is full
MrBedfordVan 0:b9164b348919 185 */
MrBedfordVan 0:b9164b348919 186 int put(ManagedString key, uint8_t* data, int dataSize);
MrBedfordVan 0:b9164b348919 187
MrBedfordVan 0:b9164b348919 188 /**
MrBedfordVan 0:b9164b348919 189 * Retreives a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 190 *
MrBedfordVan 0:b9164b348919 191 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 192 *
MrBedfordVan 0:b9164b348919 193 * @return a pointer to a heap allocated KeyValuePair struct, this pointer will be
MrBedfordVan 0:b9164b348919 194 * NULL if the key was not found in storage.
MrBedfordVan 0:b9164b348919 195 *
MrBedfordVan 0:b9164b348919 196 * @note it is up to the user to free memory after use.
MrBedfordVan 0:b9164b348919 197 */
MrBedfordVan 0:b9164b348919 198 KeyValuePair* get(const char* key);
MrBedfordVan 0:b9164b348919 199
MrBedfordVan 0:b9164b348919 200 /**
MrBedfordVan 0:b9164b348919 201 * Retreives a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 202 *
MrBedfordVan 0:b9164b348919 203 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 204 *
MrBedfordVan 0:b9164b348919 205 * @return a pointer to a heap allocated KeyValuePair struct, this pointer will be
MrBedfordVan 0:b9164b348919 206 * NULL if the key was not found in storage.
MrBedfordVan 0:b9164b348919 207 *
MrBedfordVan 0:b9164b348919 208 * @note it is up to the user to free memory after use.
MrBedfordVan 0:b9164b348919 209 */
MrBedfordVan 0:b9164b348919 210 KeyValuePair* get(ManagedString key);
MrBedfordVan 0:b9164b348919 211
MrBedfordVan 0:b9164b348919 212 /**
MrBedfordVan 0:b9164b348919 213 * Removes a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 214 *
MrBedfordVan 0:b9164b348919 215 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 216 *
MrBedfordVan 0:b9164b348919 217 * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the given key
MrBedfordVan 0:b9164b348919 218 * was not found in flash.
MrBedfordVan 0:b9164b348919 219 */
MrBedfordVan 0:b9164b348919 220 int remove(const char* key);
MrBedfordVan 0:b9164b348919 221
MrBedfordVan 0:b9164b348919 222 /**
MrBedfordVan 0:b9164b348919 223 * Removes a KeyValuePair identified by a given key.
MrBedfordVan 0:b9164b348919 224 *
MrBedfordVan 0:b9164b348919 225 * @param key the unique name used to identify a KeyValuePair in flash.
MrBedfordVan 0:b9164b348919 226 *
MrBedfordVan 0:b9164b348919 227 * @return MICROBIT_OK on success, or MICROBIT_NO_DATA if the given key
MrBedfordVan 0:b9164b348919 228 * was not found in flash.
MrBedfordVan 0:b9164b348919 229 */
MrBedfordVan 0:b9164b348919 230 int remove(ManagedString key);
MrBedfordVan 0:b9164b348919 231
MrBedfordVan 0:b9164b348919 232 /**
MrBedfordVan 0:b9164b348919 233 * The size of the flash based KeyValueStore.
MrBedfordVan 0:b9164b348919 234 *
MrBedfordVan 0:b9164b348919 235 * @return the number of entries in the key value store
MrBedfordVan 0:b9164b348919 236 */
MrBedfordVan 0:b9164b348919 237 int size();
MrBedfordVan 0:b9164b348919 238 };
MrBedfordVan 0:b9164b348919 239
MrBedfordVan 0:b9164b348919 240 #endif