Nordic stack and drivers for the mbed BLE API
Fork of nRF51822 by
TARGET_MCU_NRF51822/sdk/source/libraries/fstorage/fstorage.h@640:c90ae1400bf2, 2016-09-14 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Wed Sep 14 14:39:43 2016 +0100
- Revision:
- 640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
640:c90ae1400bf2 | 1 | /* |
Vincent Coubard |
640:c90ae1400bf2 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Vincent Coubard |
640:c90ae1400bf2 | 3 | * All rights reserved. |
Vincent Coubard |
640:c90ae1400bf2 | 4 | * |
Vincent Coubard |
640:c90ae1400bf2 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Vincent Coubard |
640:c90ae1400bf2 | 6 | * are permitted provided that the following conditions are met: |
Vincent Coubard |
640:c90ae1400bf2 | 7 | * |
Vincent Coubard |
640:c90ae1400bf2 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Vincent Coubard |
640:c90ae1400bf2 | 9 | * list of conditions and the following disclaimer. |
Vincent Coubard |
640:c90ae1400bf2 | 10 | * |
Vincent Coubard |
640:c90ae1400bf2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Vincent Coubard |
640:c90ae1400bf2 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Vincent Coubard |
640:c90ae1400bf2 | 13 | * other materials provided with the distribution. |
Vincent Coubard |
640:c90ae1400bf2 | 14 | * |
Vincent Coubard |
640:c90ae1400bf2 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Vincent Coubard |
640:c90ae1400bf2 | 16 | * contributors to this software may be used to endorse or promote products |
Vincent Coubard |
640:c90ae1400bf2 | 17 | * derived from this software without specific prior written permission. |
Vincent Coubard |
640:c90ae1400bf2 | 18 | * |
Vincent Coubard |
640:c90ae1400bf2 | 19 | * |
Vincent Coubard |
640:c90ae1400bf2 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Vincent Coubard |
640:c90ae1400bf2 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Vincent Coubard |
640:c90ae1400bf2 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Vincent Coubard |
640:c90ae1400bf2 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Vincent Coubard |
640:c90ae1400bf2 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Vincent Coubard |
640:c90ae1400bf2 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Vincent Coubard |
640:c90ae1400bf2 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Vincent Coubard |
640:c90ae1400bf2 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Vincent Coubard |
640:c90ae1400bf2 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Vincent Coubard |
640:c90ae1400bf2 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Vincent Coubard |
640:c90ae1400bf2 | 30 | * |
Vincent Coubard |
640:c90ae1400bf2 | 31 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 32 | |
Vincent Coubard |
640:c90ae1400bf2 | 33 | |
Vincent Coubard |
640:c90ae1400bf2 | 34 | #ifndef FS_H__ |
Vincent Coubard |
640:c90ae1400bf2 | 35 | #define FS_H__ |
Vincent Coubard |
640:c90ae1400bf2 | 36 | |
Vincent Coubard |
640:c90ae1400bf2 | 37 | /** @file |
Vincent Coubard |
640:c90ae1400bf2 | 38 | * |
Vincent Coubard |
640:c90ae1400bf2 | 39 | * @defgroup fstorage FStorage |
Vincent Coubard |
640:c90ae1400bf2 | 40 | * @{ |
Vincent Coubard |
640:c90ae1400bf2 | 41 | * @ingroup app_common |
Vincent Coubard |
640:c90ae1400bf2 | 42 | * @brief Module which provides low level functionality to store data to flash. |
Vincent Coubard |
640:c90ae1400bf2 | 43 | * |
Vincent Coubard |
640:c90ae1400bf2 | 44 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 45 | |
Vincent Coubard |
640:c90ae1400bf2 | 46 | |
Vincent Coubard |
640:c90ae1400bf2 | 47 | #include <stdint.h> |
Vincent Coubard |
640:c90ae1400bf2 | 48 | #include "section_vars.h" |
Vincent Coubard |
640:c90ae1400bf2 | 49 | #include "fstorage_config.h" |
Vincent Coubard |
640:c90ae1400bf2 | 50 | #include "sdk_errors.h" |
Vincent Coubard |
640:c90ae1400bf2 | 51 | |
Vincent Coubard |
640:c90ae1400bf2 | 52 | |
Vincent Coubard |
640:c90ae1400bf2 | 53 | typedef uint16_t fs_length_t; |
Vincent Coubard |
640:c90ae1400bf2 | 54 | |
Vincent Coubard |
640:c90ae1400bf2 | 55 | |
Vincent Coubard |
640:c90ae1400bf2 | 56 | typedef enum |
Vincent Coubard |
640:c90ae1400bf2 | 57 | { |
Vincent Coubard |
640:c90ae1400bf2 | 58 | FS_OP_NONE = 0, |
Vincent Coubard |
640:c90ae1400bf2 | 59 | FS_OP_STORE = 1, |
Vincent Coubard |
640:c90ae1400bf2 | 60 | FS_OP_ERASE = 2 |
Vincent Coubard |
640:c90ae1400bf2 | 61 | } fs_oper_t; |
Vincent Coubard |
640:c90ae1400bf2 | 62 | |
Vincent Coubard |
640:c90ae1400bf2 | 63 | |
Vincent Coubard |
640:c90ae1400bf2 | 64 | /**@brief Callback for flash operations. |
Vincent Coubard |
640:c90ae1400bf2 | 65 | * |
Vincent Coubard |
640:c90ae1400bf2 | 66 | * @param[in] op_code Flash access operation code. |
Vincent Coubard |
640:c90ae1400bf2 | 67 | * @param[in] result Result of the operation. |
Vincent Coubard |
640:c90ae1400bf2 | 68 | * @param[in] data Pointer to resulting data (or NULL if not in use). |
Vincent Coubard |
640:c90ae1400bf2 | 69 | * @param[in] length_words Length of data in words. |
Vincent Coubard |
640:c90ae1400bf2 | 70 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 71 | typedef void (*fs_cb_t)(uint8_t op_code, |
Vincent Coubard |
640:c90ae1400bf2 | 72 | uint32_t result, |
Vincent Coubard |
640:c90ae1400bf2 | 73 | uint32_t const * p_data, |
Vincent Coubard |
640:c90ae1400bf2 | 74 | fs_length_t length_words); |
Vincent Coubard |
640:c90ae1400bf2 | 75 | |
Vincent Coubard |
640:c90ae1400bf2 | 76 | |
Vincent Coubard |
640:c90ae1400bf2 | 77 | /**@brief Function prototype for a callback handler. |
Vincent Coubard |
640:c90ae1400bf2 | 78 | * |
Vincent Coubard |
640:c90ae1400bf2 | 79 | * @details This function is expected to be implemented by the module that |
Vincent Coubard |
640:c90ae1400bf2 | 80 | * registers for fstorage usage. Its usage is described |
Vincent Coubard |
640:c90ae1400bf2 | 81 | * in the function pointer type fs_cb_t. |
Vincent Coubard |
640:c90ae1400bf2 | 82 | * |
Vincent Coubard |
640:c90ae1400bf2 | 83 | * @param[in] op_code Flash operation code. |
Vincent Coubard |
640:c90ae1400bf2 | 84 | * @param[in] result Result of the flash operation. |
Vincent Coubard |
640:c90ae1400bf2 | 85 | * @param[in] p_data Pointer to the resulting data (or NULL if not in use). |
Vincent Coubard |
640:c90ae1400bf2 | 86 | * @param[in] length_words Length of data in words. |
Vincent Coubard |
640:c90ae1400bf2 | 87 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 88 | static void fs_callback(uint8_t op_code, |
Vincent Coubard |
640:c90ae1400bf2 | 89 | uint32_t result, |
Vincent Coubard |
640:c90ae1400bf2 | 90 | uint32_t const * p_data, |
Vincent Coubard |
640:c90ae1400bf2 | 91 | fs_length_t length_words); |
Vincent Coubard |
640:c90ae1400bf2 | 92 | |
Vincent Coubard |
640:c90ae1400bf2 | 93 | |
Vincent Coubard |
640:c90ae1400bf2 | 94 | /**@brief Flash storage config variable. |
Vincent Coubard |
640:c90ae1400bf2 | 95 | * |
Vincent Coubard |
640:c90ae1400bf2 | 96 | * @details The fstorage module will update the start_addr and end_address according to |
Vincent Coubard |
640:c90ae1400bf2 | 97 | * ordering rules and the number of pages requested by the fstorage module user. |
Vincent Coubard |
640:c90ae1400bf2 | 98 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 99 | typedef struct |
Vincent Coubard |
640:c90ae1400bf2 | 100 | { |
Vincent Coubard |
640:c90ae1400bf2 | 101 | const fs_cb_t cb; /**< Callback to run when flash operation has completed. */ |
Vincent Coubard |
640:c90ae1400bf2 | 102 | const uint8_t num_pages; /**< The number of pages to reserve for flash storage. */ |
Vincent Coubard |
640:c90ae1400bf2 | 103 | const uint8_t page_order; /**< The order used to allocate pages. */ |
Vincent Coubard |
640:c90ae1400bf2 | 104 | uint32_t * p_start_addr; /**< Pointer to the start address of the allocated flash storage. Set by running @ref fs_init. */ |
Vincent Coubard |
640:c90ae1400bf2 | 105 | uint32_t * p_end_addr; /**< Pointer to the end address of the allcoated flash storage. Set by running @ref fs_init. */ |
Vincent Coubard |
640:c90ae1400bf2 | 106 | } fs_config_t; |
Vincent Coubard |
640:c90ae1400bf2 | 107 | |
Vincent Coubard |
640:c90ae1400bf2 | 108 | |
Vincent Coubard |
640:c90ae1400bf2 | 109 | /**@brief Macro for registering of flash storage configuration variable. |
Vincent Coubard |
640:c90ae1400bf2 | 110 | * |
Vincent Coubard |
640:c90ae1400bf2 | 111 | * @details This macro is expected to be invoked in the code unit that that require |
Vincent Coubard |
640:c90ae1400bf2 | 112 | * flash storage. Invoking this places the registered configuration variable |
Vincent Coubard |
640:c90ae1400bf2 | 113 | * in a section named "fs_data" that the fstorage module uses during initialization |
Vincent Coubard |
640:c90ae1400bf2 | 114 | * and regular operation. |
Vincent Coubard |
640:c90ae1400bf2 | 115 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 116 | #define FS_SECTION_VARS_ADD(type_def) NRF_SECTION_VARS_ADD(fs_data, type_def) |
Vincent Coubard |
640:c90ae1400bf2 | 117 | |
Vincent Coubard |
640:c90ae1400bf2 | 118 | |
Vincent Coubard |
640:c90ae1400bf2 | 119 | /**@brief Function to initialize FStorage. |
Vincent Coubard |
640:c90ae1400bf2 | 120 | * |
Vincent Coubard |
640:c90ae1400bf2 | 121 | * @details This function allocates flash data pages according to the |
Vincent Coubard |
640:c90ae1400bf2 | 122 | * number requested in the config variable. The data used to initialize. |
Vincent Coubard |
640:c90ae1400bf2 | 123 | * the fstorage is section placed variables in the data section "fs_data". |
Vincent Coubard |
640:c90ae1400bf2 | 124 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 125 | ret_code_t fs_init(void); |
Vincent Coubard |
640:c90ae1400bf2 | 126 | |
Vincent Coubard |
640:c90ae1400bf2 | 127 | |
Vincent Coubard |
640:c90ae1400bf2 | 128 | /**@brief Function to store data in flash. |
Vincent Coubard |
640:c90ae1400bf2 | 129 | * |
Vincent Coubard |
640:c90ae1400bf2 | 130 | * @warning The data to be written to flash has to be kept in memory until the operation has |
Vincent Coubard |
640:c90ae1400bf2 | 131 | * terminated, i.e., a callback is received. |
Vincent Coubard |
640:c90ae1400bf2 | 132 | * |
Vincent Coubard |
640:c90ae1400bf2 | 133 | * @param[in] p_config Const pointer to configiguration of module user that requests a store operation. |
Vincent Coubard |
640:c90ae1400bf2 | 134 | * @param[in] p_addr Write address of store operation. |
Vincent Coubard |
640:c90ae1400bf2 | 135 | * @param[in] p_data Pointer to the data to store. |
Vincent Coubard |
640:c90ae1400bf2 | 136 | * @param[in] length_words Length of the data to store. |
Vincent Coubard |
640:c90ae1400bf2 | 137 | * |
Vincent Coubard |
640:c90ae1400bf2 | 138 | * @retval NRF_SUCCESS Success. Command queued. |
Vincent Coubard |
640:c90ae1400bf2 | 139 | * @retval NRF_ERROR_INVALID_STATE Error. The module is not initialized. |
Vincent Coubard |
640:c90ae1400bf2 | 140 | * @retval NRF_ERROR_INVALID_ADDR Error. Data is unaligned or invalid configuration. |
Vincent Coubard |
640:c90ae1400bf2 | 141 | * @retval Any error returned by the SoftDevice flash API. |
Vincent Coubard |
640:c90ae1400bf2 | 142 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 143 | ret_code_t fs_store(fs_config_t const * p_config, |
Vincent Coubard |
640:c90ae1400bf2 | 144 | uint32_t const * p_addr, |
Vincent Coubard |
640:c90ae1400bf2 | 145 | uint32_t const * const p_data, |
Vincent Coubard |
640:c90ae1400bf2 | 146 | fs_length_t length_words); |
Vincent Coubard |
640:c90ae1400bf2 | 147 | |
Vincent Coubard |
640:c90ae1400bf2 | 148 | |
Vincent Coubard |
640:c90ae1400bf2 | 149 | /** Function to erase a page in flash. |
Vincent Coubard |
640:c90ae1400bf2 | 150 | * |
Vincent Coubard |
640:c90ae1400bf2 | 151 | * @note The erase address must be aligned on a page boundary. The length in words must be |
Vincent Coubard |
640:c90ae1400bf2 | 152 | * equivalent to the page size. |
Vincent Coubard |
640:c90ae1400bf2 | 153 | * |
Vincent Coubard |
640:c90ae1400bf2 | 154 | * @param[in] p_config Pointer to the configuration of the user that requests the operation. |
Vincent Coubard |
640:c90ae1400bf2 | 155 | * @param[in] p_addr Address of page to erase (the same as first word in the page). |
Vincent Coubard |
640:c90ae1400bf2 | 156 | * @param[in] length_words Length (in 4 byte words) of the area to erase. |
Vincent Coubard |
640:c90ae1400bf2 | 157 | * |
Vincent Coubard |
640:c90ae1400bf2 | 158 | * @retval NRF_SUCCESS Success. Command queued. |
Vincent Coubard |
640:c90ae1400bf2 | 159 | * @retval NRF_ERROR_INVALID_STATE Error. The module is not initialized. |
Vincent Coubard |
640:c90ae1400bf2 | 160 | * @retval NRF_ERROR_INVALID_ADDR Error. Data is unaligned or invalid configuration. |
Vincent Coubard |
640:c90ae1400bf2 | 161 | * @retval Any error returned by the SoftDevice flash API. |
Vincent Coubard |
640:c90ae1400bf2 | 162 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 163 | ret_code_t fs_erase(fs_config_t const * p_config, |
Vincent Coubard |
640:c90ae1400bf2 | 164 | uint32_t * const p_addr, |
Vincent Coubard |
640:c90ae1400bf2 | 165 | fs_length_t length_words); |
Vincent Coubard |
640:c90ae1400bf2 | 166 | |
Vincent Coubard |
640:c90ae1400bf2 | 167 | |
Vincent Coubard |
640:c90ae1400bf2 | 168 | /**@brief Function to call to handle events from the SoftDevice |
Vincent Coubard |
640:c90ae1400bf2 | 169 | * |
Vincent Coubard |
640:c90ae1400bf2 | 170 | * @param sys_evt System event from the SoftDevice |
Vincent Coubard |
640:c90ae1400bf2 | 171 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 172 | void fs_sys_event_handler(uint32_t sys_evt); |
Vincent Coubard |
640:c90ae1400bf2 | 173 | |
Vincent Coubard |
640:c90ae1400bf2 | 174 | /** @} */ |
Vincent Coubard |
640:c90ae1400bf2 | 175 | |
Vincent Coubard |
640:c90ae1400bf2 | 176 | #endif // FS_H__ |