Theo Havercroft / nrf51-sdk

Fork of nrf51-sdk by Lancaster University

Committer:
Jonathan Austin
Date:
Wed Apr 06 23:55:04 2016 +0100
Revision:
0:bc2961fa1ef0
Synchronized with git rev 90647e3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 0:bc2961fa1ef0 1 /*
Jonathan Austin 0:bc2961fa1ef0 2 * Copyright (c) Nordic Semiconductor ASA
Jonathan Austin 0:bc2961fa1ef0 3 * All rights reserved.
Jonathan Austin 0:bc2961fa1ef0 4 *
Jonathan Austin 0:bc2961fa1ef0 5 * Redistribution and use in source and binary forms, with or without modification,
Jonathan Austin 0:bc2961fa1ef0 6 * are permitted provided that the following conditions are met:
Jonathan Austin 0:bc2961fa1ef0 7 *
Jonathan Austin 0:bc2961fa1ef0 8 * 1. Redistributions of source code must retain the above copyright notice, this
Jonathan Austin 0:bc2961fa1ef0 9 * list of conditions and the following disclaimer.
Jonathan Austin 0:bc2961fa1ef0 10 *
Jonathan Austin 0:bc2961fa1ef0 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Jonathan Austin 0:bc2961fa1ef0 12 * list of conditions and the following disclaimer in the documentation and/or
Jonathan Austin 0:bc2961fa1ef0 13 * other materials provided with the distribution.
Jonathan Austin 0:bc2961fa1ef0 14 *
Jonathan Austin 0:bc2961fa1ef0 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Jonathan Austin 0:bc2961fa1ef0 16 * contributors to this software may be used to endorse or promote products
Jonathan Austin 0:bc2961fa1ef0 17 * derived from this software without specific prior written permission.
Jonathan Austin 0:bc2961fa1ef0 18 *
Jonathan Austin 0:bc2961fa1ef0 19 *
Jonathan Austin 0:bc2961fa1ef0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Jonathan Austin 0:bc2961fa1ef0 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Jonathan Austin 0:bc2961fa1ef0 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Jonathan Austin 0:bc2961fa1ef0 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Jonathan Austin 0:bc2961fa1ef0 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Jonathan Austin 0:bc2961fa1ef0 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Jonathan Austin 0:bc2961fa1ef0 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Jonathan Austin 0:bc2961fa1ef0 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Jonathan Austin 0:bc2961fa1ef0 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Jonathan Austin 0:bc2961fa1ef0 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jonathan Austin 0:bc2961fa1ef0 30 *
Jonathan Austin 0:bc2961fa1ef0 31 */
Jonathan Austin 0:bc2961fa1ef0 32
Jonathan Austin 0:bc2961fa1ef0 33 #ifndef FDS_TYPES_INTERNAL__
Jonathan Austin 0:bc2961fa1ef0 34 #define FDS_TYPES_INTERNAL__
Jonathan Austin 0:bc2961fa1ef0 35
Jonathan Austin 0:bc2961fa1ef0 36 #include "fds.h"
Jonathan Austin 0:bc2961fa1ef0 37 #include <stdint.h>
Jonathan Austin 0:bc2961fa1ef0 38 #include <stdbool.h>
Jonathan Austin 0:bc2961fa1ef0 39 #include "nrf_soc.h"
Jonathan Austin 0:bc2961fa1ef0 40
Jonathan Austin 0:bc2961fa1ef0 41
Jonathan Austin 0:bc2961fa1ef0 42 #define COMMAND_EXECUTING (NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 43 #define COMMAND_COMPLETED (0x1234)
Jonathan Austin 0:bc2961fa1ef0 44 //#define COMMAND_FAILED (0x1236)
Jonathan Austin 0:bc2961fa1ef0 45
Jonathan Austin 0:bc2961fa1ef0 46 #define FDS_MAGIC_HWORD (0xF11E)
Jonathan Austin 0:bc2961fa1ef0 47 #define FDS_MAGIC_WORD (0x15ABE11A)
Jonathan Austin 0:bc2961fa1ef0 48 #define FDS_ERASED_WORD (0xFFFFFFFF)
Jonathan Austin 0:bc2961fa1ef0 49
Jonathan Austin 0:bc2961fa1ef0 50 #define FDS_PAGE_TAG_SIZE (4) /**< Page tag size, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 51
Jonathan Austin 0:bc2961fa1ef0 52 #define FDS_VPAGE_ID_UNKNOWN (0xFFFF)
Jonathan Austin 0:bc2961fa1ef0 53
Jonathan Austin 0:bc2961fa1ef0 54 #define FDS_WRITE_OFFSET_TL (0) /**< Offset of TL from the record base address, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 55 #define FDS_WRITE_OFFSET_IC (1) /**< Offset of IC from the record base address, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 56 #define FDS_WRITE_OFFSET_ID (2) /**< Offset of ID from the record base address, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 57 #define FDS_WRITE_OFFSET_DATA (3) /**< Offset of the data (chunks) from the record base address, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 58
Jonathan Austin 0:bc2961fa1ef0 59 #define FDS_HEADER_SIZE_TL (1) /**< Size of the TL part of the header, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 60 #define FDS_HEADER_SIZE_ID (1) /**< Size of the IC part of the header, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 61 #define FDS_HEADER_SIZE_IC (1) /**< Size of the IC part of the header, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 62 #define FDS_HEADER_SIZE (3) /**< Size of the whole header, in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 63
Jonathan Austin 0:bc2961fa1ef0 64 #define FDS_CMD_QUEUE_SIZE_INIT (1)
Jonathan Austin 0:bc2961fa1ef0 65 #define FDS_CMD_QUEUE_SIZE_WRITE (1)
Jonathan Austin 0:bc2961fa1ef0 66 #define FDS_CMD_QUEUE_SIZE_CLEAR (1)
Jonathan Austin 0:bc2961fa1ef0 67 #define FDS_CMD_QUEUE_SIZE_UPDATE (2)
Jonathan Austin 0:bc2961fa1ef0 68 #define FDS_CMD_QUEUE_SIZE_GC (1)
Jonathan Austin 0:bc2961fa1ef0 69
Jonathan Austin 0:bc2961fa1ef0 70
Jonathan Austin 0:bc2961fa1ef0 71 static uint8_t m_nested_critical;
Jonathan Austin 0:bc2961fa1ef0 72
Jonathan Austin 0:bc2961fa1ef0 73 /** Macros to enable and disable application interrupts. */
Jonathan Austin 0:bc2961fa1ef0 74 #define CRITICAL_SECTION_ENTER() //sd_nvic_critical_region_enter(&m_nested_critical)
Jonathan Austin 0:bc2961fa1ef0 75 #define CRITICAL_SECTION_EXIT() //sd_nvic_critical_region_exit ( m_nested_critical)
Jonathan Austin 0:bc2961fa1ef0 76
Jonathan Austin 0:bc2961fa1ef0 77 /**@brief Page types. */
Jonathan Austin 0:bc2961fa1ef0 78 typedef enum
Jonathan Austin 0:bc2961fa1ef0 79 {
Jonathan Austin 0:bc2961fa1ef0 80 FDS_PAGE_UNDEFINED, /**< Undefined page type. */
Jonathan Austin 0:bc2961fa1ef0 81 FDS_PAGE_ERASED, /**< Page is erased. */
Jonathan Austin 0:bc2961fa1ef0 82 FDS_PAGE_VALID, /**< Page is ready for storage. */
Jonathan Austin 0:bc2961fa1ef0 83 FDS_PAGE_SWAP, /**< Page is reserved for GC. */
Jonathan Austin 0:bc2961fa1ef0 84 FDS_PAGE_GC /**< Page is being garbage collected. */
Jonathan Austin 0:bc2961fa1ef0 85 } fds_page_type_t;
Jonathan Austin 0:bc2961fa1ef0 86
Jonathan Austin 0:bc2961fa1ef0 87
Jonathan Austin 0:bc2961fa1ef0 88 typedef enum
Jonathan Austin 0:bc2961fa1ef0 89 {
Jonathan Austin 0:bc2961fa1ef0 90 FDS_OP_NONE = 0x00, /**< No operation. */
Jonathan Austin 0:bc2961fa1ef0 91 FDS_OP_WRITE_TL, /**< Write the type and length. */
Jonathan Austin 0:bc2961fa1ef0 92 FDS_OP_WRITE_ID, /**< Write the record ID. */
Jonathan Austin 0:bc2961fa1ef0 93 FDS_OP_WRITE_CHUNK, /**< Write the record value. */
Jonathan Austin 0:bc2961fa1ef0 94 FDS_OP_WRITE_IC, /**< Write the instance and checksum. */
Jonathan Austin 0:bc2961fa1ef0 95 FDS_OP_CLEAR_TL,
Jonathan Austin 0:bc2961fa1ef0 96 FDS_OP_CLEAR_INSTANCE,
Jonathan Austin 0:bc2961fa1ef0 97 FDS_OP_DONE,
Jonathan Austin 0:bc2961fa1ef0 98 } fds_opcode_t;
Jonathan Austin 0:bc2961fa1ef0 99
Jonathan Austin 0:bc2961fa1ef0 100
Jonathan Austin 0:bc2961fa1ef0 101 typedef enum
Jonathan Austin 0:bc2961fa1ef0 102 {
Jonathan Austin 0:bc2961fa1ef0 103 FDS_FLAG_INITIALIZING = (1 << 0), /**< TODO: Not really needed atm? */
Jonathan Austin 0:bc2961fa1ef0 104 FDS_FLAG_INITIALIZED = (1 << 1), /**< Flag indicating that flash data storage has been initialized. */
Jonathan Austin 0:bc2961fa1ef0 105 FDS_FLAG_PROCESSING = (1 << 2), /**< Flag indicating that queue is being processed. */
Jonathan Austin 0:bc2961fa1ef0 106 FDS_FLAG_CAN_GC = (1 << 3), /**< Flag indicating that fds can regain data by performing garbage collection. */
Jonathan Austin 0:bc2961fa1ef0 107 } fds_flags_t;
Jonathan Austin 0:bc2961fa1ef0 108
Jonathan Austin 0:bc2961fa1ef0 109
Jonathan Austin 0:bc2961fa1ef0 110 typedef struct
Jonathan Austin 0:bc2961fa1ef0 111 {
Jonathan Austin 0:bc2961fa1ef0 112 uint32_t const * start_addr;
Jonathan Austin 0:bc2961fa1ef0 113 uint16_t vpage_id; /**< The page logical ID. */
Jonathan Austin 0:bc2961fa1ef0 114 uint16_t volatile write_offset; /**< The page write offset, in 4 bytes words. */
Jonathan Austin 0:bc2961fa1ef0 115 uint16_t volatile words_reserved; /**< The amount of words reserved by fds_write_reserve() on this page. */
Jonathan Austin 0:bc2961fa1ef0 116 uint16_t volatile records_open;
Jonathan Austin 0:bc2961fa1ef0 117 fds_page_type_t page_type : 4; /**< The page type. */
Jonathan Austin 0:bc2961fa1ef0 118 } fds_page_t;
Jonathan Austin 0:bc2961fa1ef0 119
Jonathan Austin 0:bc2961fa1ef0 120
Jonathan Austin 0:bc2961fa1ef0 121 typedef struct
Jonathan Austin 0:bc2961fa1ef0 122 {
Jonathan Austin 0:bc2961fa1ef0 123 fds_cmd_id_t id : 4; /**< The ID of the command. */
Jonathan Austin 0:bc2961fa1ef0 124 fds_opcode_t op_code : 4;
Jonathan Austin 0:bc2961fa1ef0 125 uint8_t num_chunks; /**< Number of operations this command has left in the operation queue. */
Jonathan Austin 0:bc2961fa1ef0 126 uint16_t chunk_offset; /**< Offset used for writing the record value(s), in 4 byte words. */
Jonathan Austin 0:bc2961fa1ef0 127 uint16_t vpage_id; /**< The virtual page ID where we reserved the flash space for this command. */
Jonathan Austin 0:bc2961fa1ef0 128 fds_record_header_t record_header;
Jonathan Austin 0:bc2961fa1ef0 129 } fds_cmd_t;
Jonathan Austin 0:bc2961fa1ef0 130
Jonathan Austin 0:bc2961fa1ef0 131
Jonathan Austin 0:bc2961fa1ef0 132 /**@brief Defines command queue, an element is free if the op_code field is not invalid.
Jonathan Austin 0:bc2961fa1ef0 133 *
Jonathan Austin 0:bc2961fa1ef0 134 * @details Defines commands enqueued for flash access. At any point in time, this queue has one or
Jonathan Austin 0:bc2961fa1ef0 135 * more flash access operations pending if the count field is not zero. When the queue is
Jonathan Austin 0:bc2961fa1ef0 136 * not empty, the rp (read pointer) field points to the flash access command in progress
Jonathan Austin 0:bc2961fa1ef0 137 * or, if none is in progress, the command to be requested next. The queue implements a
Jonathan Austin 0:bc2961fa1ef0 138 * simple first in first out algorithm. Data addresses are assumed to be resident.
Jonathan Austin 0:bc2961fa1ef0 139 */
Jonathan Austin 0:bc2961fa1ef0 140 typedef struct
Jonathan Austin 0:bc2961fa1ef0 141 {
Jonathan Austin 0:bc2961fa1ef0 142 fds_cmd_t cmd[FDS_CMD_QUEUE_SIZE]; /**< Array to maintain flash access operation details. */
Jonathan Austin 0:bc2961fa1ef0 143 uint8_t volatile rp; /**< The index of the command being executed. */
Jonathan Austin 0:bc2961fa1ef0 144 uint8_t volatile count; /**< Number of elements in the queue. */
Jonathan Austin 0:bc2961fa1ef0 145 } fds_cmd_queue_t;
Jonathan Austin 0:bc2961fa1ef0 146
Jonathan Austin 0:bc2961fa1ef0 147
Jonathan Austin 0:bc2961fa1ef0 148 typedef struct
Jonathan Austin 0:bc2961fa1ef0 149 {
Jonathan Austin 0:bc2961fa1ef0 150 fds_record_chunk_t chunk[FDS_CHUNK_QUEUE_SIZE];
Jonathan Austin 0:bc2961fa1ef0 151 uint8_t volatile rp;
Jonathan Austin 0:bc2961fa1ef0 152 uint8_t volatile count;
Jonathan Austin 0:bc2961fa1ef0 153 } fds_chunk_queue_t;
Jonathan Austin 0:bc2961fa1ef0 154
Jonathan Austin 0:bc2961fa1ef0 155
Jonathan Austin 0:bc2961fa1ef0 156 typedef enum
Jonathan Austin 0:bc2961fa1ef0 157 {
Jonathan Austin 0:bc2961fa1ef0 158 NONE,
Jonathan Austin 0:bc2961fa1ef0 159 BEGIN,
Jonathan Austin 0:bc2961fa1ef0 160 RESUME,
Jonathan Austin 0:bc2961fa1ef0 161 GC_PAGE,
Jonathan Austin 0:bc2961fa1ef0 162 COPY_RECORD,
Jonathan Austin 0:bc2961fa1ef0 163 READY_SWAP,
Jonathan Austin 0:bc2961fa1ef0 164 NEW_SWAP,
Jonathan Austin 0:bc2961fa1ef0 165 INIT_SWAP
Jonathan Austin 0:bc2961fa1ef0 166 } fds_gc_state_t;
Jonathan Austin 0:bc2961fa1ef0 167
Jonathan Austin 0:bc2961fa1ef0 168
Jonathan Austin 0:bc2961fa1ef0 169 typedef struct
Jonathan Austin 0:bc2961fa1ef0 170 {
Jonathan Austin 0:bc2961fa1ef0 171 uint16_t cur_page;
Jonathan Austin 0:bc2961fa1ef0 172 uint16_t swap_page;
Jonathan Austin 0:bc2961fa1ef0 173 uint32_t const * p_scan_addr;
Jonathan Austin 0:bc2961fa1ef0 174 fds_gc_state_t state;
Jonathan Austin 0:bc2961fa1ef0 175 bool do_gc_page[FDS_MAX_PAGES];
Jonathan Austin 0:bc2961fa1ef0 176 } fds_gc_data_t;
Jonathan Austin 0:bc2961fa1ef0 177
Jonathan Austin 0:bc2961fa1ef0 178 #endif // FDS_TYPES_INTERNAL__