Nordic stack and drivers for the mbed BLE API
Dependents: Sensen-classic-v8-IAQ_copy_4
Fork of nRF51822 by
TARGET_MCU_NRF51822/sdk/source/drivers_nrf/ble_flash/ble_flash.h@639:7f459d371b75, 2017-11-10 (annotated)
- Committer:
- amithy
- Date:
- Fri Nov 10 20:27:44 2017 +0000
- Revision:
- 639:7f459d371b75
- Parent:
- 638:c90ae1400bf2
t
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
638:c90ae1400bf2 | 1 | /* |
Vincent Coubard |
638:c90ae1400bf2 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Vincent Coubard |
638:c90ae1400bf2 | 3 | * All rights reserved. |
Vincent Coubard |
638:c90ae1400bf2 | 4 | * |
Vincent Coubard |
638:c90ae1400bf2 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Vincent Coubard |
638:c90ae1400bf2 | 6 | * are permitted provided that the following conditions are met: |
Vincent Coubard |
638:c90ae1400bf2 | 7 | * |
Vincent Coubard |
638:c90ae1400bf2 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Vincent Coubard |
638:c90ae1400bf2 | 9 | * list of conditions and the following disclaimer. |
Vincent Coubard |
638:c90ae1400bf2 | 10 | * |
Vincent Coubard |
638:c90ae1400bf2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Vincent Coubard |
638:c90ae1400bf2 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Vincent Coubard |
638:c90ae1400bf2 | 13 | * other materials provided with the distribution. |
Vincent Coubard |
638:c90ae1400bf2 | 14 | * |
Vincent Coubard |
638:c90ae1400bf2 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Vincent Coubard |
638:c90ae1400bf2 | 16 | * contributors to this software may be used to endorse or promote products |
Vincent Coubard |
638:c90ae1400bf2 | 17 | * derived from this software without specific prior written permission. |
Vincent Coubard |
638:c90ae1400bf2 | 18 | * |
Vincent Coubard |
638:c90ae1400bf2 | 19 | * |
Vincent Coubard |
638:c90ae1400bf2 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Vincent Coubard |
638:c90ae1400bf2 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Vincent Coubard |
638:c90ae1400bf2 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Vincent Coubard |
638:c90ae1400bf2 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Vincent Coubard |
638:c90ae1400bf2 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Vincent Coubard |
638:c90ae1400bf2 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Vincent Coubard |
638:c90ae1400bf2 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Vincent Coubard |
638:c90ae1400bf2 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Vincent Coubard |
638:c90ae1400bf2 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Vincent Coubard |
638:c90ae1400bf2 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Vincent Coubard |
638:c90ae1400bf2 | 30 | * |
Vincent Coubard |
638:c90ae1400bf2 | 31 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 32 | |
Vincent Coubard |
638:c90ae1400bf2 | 33 | /** @file |
Vincent Coubard |
638:c90ae1400bf2 | 34 | * |
Vincent Coubard |
638:c90ae1400bf2 | 35 | * @defgroup ble_flash_module Flash Manager |
Vincent Coubard |
638:c90ae1400bf2 | 36 | * @{ |
Vincent Coubard |
638:c90ae1400bf2 | 37 | * @ingroup ble_sdk_lib |
Vincent Coubard |
638:c90ae1400bf2 | 38 | * @brief Module for accessing flash memory. |
Vincent Coubard |
638:c90ae1400bf2 | 39 | * |
Vincent Coubard |
638:c90ae1400bf2 | 40 | * @details It contains functions for reading, writing and erasing one page in flash. |
Vincent Coubard |
638:c90ae1400bf2 | 41 | * |
Vincent Coubard |
638:c90ae1400bf2 | 42 | * The module uses the first 32 bits of the flash page to write a magic number in order to |
Vincent Coubard |
638:c90ae1400bf2 | 43 | * determine if the page has been written or not. |
Vincent Coubard |
638:c90ae1400bf2 | 44 | * |
Vincent Coubard |
638:c90ae1400bf2 | 45 | * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the |
Vincent Coubard |
638:c90ae1400bf2 | 46 | * range 0 to 127), or in your application space! In both cases, this would end up |
Vincent Coubard |
638:c90ae1400bf2 | 47 | * with a hard fault. |
Vincent Coubard |
638:c90ae1400bf2 | 48 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 49 | |
Vincent Coubard |
638:c90ae1400bf2 | 50 | #ifndef BLE_FLASH_H__ |
Vincent Coubard |
638:c90ae1400bf2 | 51 | #define BLE_FLASH_H__ |
Vincent Coubard |
638:c90ae1400bf2 | 52 | |
Vincent Coubard |
638:c90ae1400bf2 | 53 | #include <stdint.h> |
Vincent Coubard |
638:c90ae1400bf2 | 54 | #include <stdbool.h> |
Vincent Coubard |
638:c90ae1400bf2 | 55 | #include "nrf.h" |
Vincent Coubard |
638:c90ae1400bf2 | 56 | |
Vincent Coubard |
638:c90ae1400bf2 | 57 | #define BLE_FLASH_PAGE_SIZE ((uint16_t)NRF_FICR->CODEPAGESIZE) /**< Size of one flash page. */ |
Vincent Coubard |
638:c90ae1400bf2 | 58 | #define BLE_FLASH_MAGIC_NUMBER 0x45DE0000 /**< Magic value to identify if flash contains valid data. */ |
Vincent Coubard |
638:c90ae1400bf2 | 59 | #define BLE_FLASH_EMPTY_MASK 0xFFFFFFFF /**< Bit mask that defines an empty address in flash. */ |
Vincent Coubard |
638:c90ae1400bf2 | 60 | |
Vincent Coubard |
638:c90ae1400bf2 | 61 | |
Vincent Coubard |
638:c90ae1400bf2 | 62 | /**@brief Macro for getting the end of the flash available for application. |
Vincent Coubard |
638:c90ae1400bf2 | 63 | * |
Vincent Coubard |
638:c90ae1400bf2 | 64 | * @details The result flash page number indicates the end boundary of the flash available |
Vincent Coubard |
638:c90ae1400bf2 | 65 | * to the application. If a bootloader is used, the end will be the start of the |
Vincent Coubard |
638:c90ae1400bf2 | 66 | * bootloader region. Otherwise, the end will be the size of the flash. |
Vincent Coubard |
638:c90ae1400bf2 | 67 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 68 | #define BLE_FLASH_PAGE_END \ |
Vincent Coubard |
638:c90ae1400bf2 | 69 | ((NRF_UICR->BOOTLOADERADDR != BLE_FLASH_EMPTY_MASK) \ |
Vincent Coubard |
638:c90ae1400bf2 | 70 | ? (NRF_UICR->BOOTLOADERADDR / BLE_FLASH_PAGE_SIZE) \ |
Vincent Coubard |
638:c90ae1400bf2 | 71 | : NRF_FICR->CODESIZE) |
Vincent Coubard |
638:c90ae1400bf2 | 72 | |
Vincent Coubard |
638:c90ae1400bf2 | 73 | /**@brief Function for erasing the specified flash page, and then writes the given data to this page. |
Vincent Coubard |
638:c90ae1400bf2 | 74 | * |
Vincent Coubard |
638:c90ae1400bf2 | 75 | * @warning This operation blocks the CPU. DO NOT use while in a connection! |
Vincent Coubard |
638:c90ae1400bf2 | 76 | * |
Vincent Coubard |
638:c90ae1400bf2 | 77 | * @param[in] page_num Page number to update. |
Vincent Coubard |
638:c90ae1400bf2 | 78 | * @param[in] p_in_array Pointer to a RAM area containing the elements to write in flash. |
Vincent Coubard |
638:c90ae1400bf2 | 79 | * This area has to be 32 bits aligned. |
Vincent Coubard |
638:c90ae1400bf2 | 80 | * @param[in] word_count Number of 32 bits words to write in flash. |
Vincent Coubard |
638:c90ae1400bf2 | 81 | * |
Vincent Coubard |
638:c90ae1400bf2 | 82 | * @return NRF_SUCCESS on successful flash write, otherwise an error code. |
Vincent Coubard |
638:c90ae1400bf2 | 83 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 84 | uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count); |
Vincent Coubard |
638:c90ae1400bf2 | 85 | |
Vincent Coubard |
638:c90ae1400bf2 | 86 | /**@brief Function for reading data from flash to RAM. |
Vincent Coubard |
638:c90ae1400bf2 | 87 | * |
Vincent Coubard |
638:c90ae1400bf2 | 88 | * @param[in] page_num Page number to read. |
Vincent Coubard |
638:c90ae1400bf2 | 89 | * @param[out] p_out_array Pointer to a RAM area where the found data will be written. |
Vincent Coubard |
638:c90ae1400bf2 | 90 | * This area has to be 32 bits aligned. |
Vincent Coubard |
638:c90ae1400bf2 | 91 | * @param[out] p_word_count Number of 32 bits words read. |
Vincent Coubard |
638:c90ae1400bf2 | 92 | * |
Vincent Coubard |
638:c90ae1400bf2 | 93 | * @return NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found |
Vincent Coubard |
638:c90ae1400bf2 | 94 | * in flash (first 32 bits not equal to the MAGIC_NUMBER+CRC). |
Vincent Coubard |
638:c90ae1400bf2 | 95 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 96 | uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count); |
Vincent Coubard |
638:c90ae1400bf2 | 97 | |
Vincent Coubard |
638:c90ae1400bf2 | 98 | /**@brief Function for erasing a flash page. |
Vincent Coubard |
638:c90ae1400bf2 | 99 | * |
Vincent Coubard |
638:c90ae1400bf2 | 100 | * @note This operation blocks the CPU, so it should not be done while the radio is running! |
Vincent Coubard |
638:c90ae1400bf2 | 101 | * |
Vincent Coubard |
638:c90ae1400bf2 | 102 | * @param[in] page_num Page number to erase. |
Vincent Coubard |
638:c90ae1400bf2 | 103 | * |
Vincent Coubard |
638:c90ae1400bf2 | 104 | * @return NRF_SUCCESS on success, an error_code otherwise. |
Vincent Coubard |
638:c90ae1400bf2 | 105 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 106 | uint32_t ble_flash_page_erase(uint8_t page_num); |
Vincent Coubard |
638:c90ae1400bf2 | 107 | |
Vincent Coubard |
638:c90ae1400bf2 | 108 | /**@brief Function for writing one word to flash. |
Vincent Coubard |
638:c90ae1400bf2 | 109 | * |
Vincent Coubard |
638:c90ae1400bf2 | 110 | * @note Flash location to be written must have been erased previously. |
Vincent Coubard |
638:c90ae1400bf2 | 111 | * |
Vincent Coubard |
638:c90ae1400bf2 | 112 | * @param[in] p_address Pointer to flash location to be written. |
Vincent Coubard |
638:c90ae1400bf2 | 113 | * @param[in] value Value to write to flash. |
Vincent Coubard |
638:c90ae1400bf2 | 114 | * |
Vincent Coubard |
638:c90ae1400bf2 | 115 | * @return NRF_SUCCESS. |
Vincent Coubard |
638:c90ae1400bf2 | 116 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 117 | uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value); |
Vincent Coubard |
638:c90ae1400bf2 | 118 | |
Vincent Coubard |
638:c90ae1400bf2 | 119 | /**@brief Function for writing a data block to flash. |
Vincent Coubard |
638:c90ae1400bf2 | 120 | * |
Vincent Coubard |
638:c90ae1400bf2 | 121 | * @note Flash locations to be written must have been erased previously. |
Vincent Coubard |
638:c90ae1400bf2 | 122 | * |
Vincent Coubard |
638:c90ae1400bf2 | 123 | * @param[in] p_address Pointer to start of flash location to be written. |
Vincent Coubard |
638:c90ae1400bf2 | 124 | * @param[in] p_in_array Pointer to start of flash block to be written. |
Vincent Coubard |
638:c90ae1400bf2 | 125 | * @param[in] word_count Number of words to be written. |
Vincent Coubard |
638:c90ae1400bf2 | 126 | * |
Vincent Coubard |
638:c90ae1400bf2 | 127 | * @return NRF_SUCCESS. |
Vincent Coubard |
638:c90ae1400bf2 | 128 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 129 | uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count); |
Vincent Coubard |
638:c90ae1400bf2 | 130 | |
Vincent Coubard |
638:c90ae1400bf2 | 131 | /**@brief Function for computing pointer to start of specified flash page. |
Vincent Coubard |
638:c90ae1400bf2 | 132 | * |
Vincent Coubard |
638:c90ae1400bf2 | 133 | * @param[in] page_num Page number. |
Vincent Coubard |
638:c90ae1400bf2 | 134 | * @param[out] pp_page_addr Pointer to start of flash page. |
Vincent Coubard |
638:c90ae1400bf2 | 135 | * |
Vincent Coubard |
638:c90ae1400bf2 | 136 | * @return NRF_SUCCESS. |
Vincent Coubard |
638:c90ae1400bf2 | 137 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 138 | uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr); |
Vincent Coubard |
638:c90ae1400bf2 | 139 | |
Vincent Coubard |
638:c90ae1400bf2 | 140 | /**@brief Function for calculating a 16 bit CRC using the CRC-16-CCITT scheme. |
Vincent Coubard |
638:c90ae1400bf2 | 141 | * |
Vincent Coubard |
638:c90ae1400bf2 | 142 | * @param[in] p_data Pointer to data on which the CRC is to be calulated. |
Vincent Coubard |
638:c90ae1400bf2 | 143 | * @param[in] size Number of bytes on which the CRC is to be calulated. |
Vincent Coubard |
638:c90ae1400bf2 | 144 | * @param[in] p_crc Initial CRC value (if NULL, a preset value is used as the initial value). |
Vincent Coubard |
638:c90ae1400bf2 | 145 | * |
Vincent Coubard |
638:c90ae1400bf2 | 146 | * @return Calculated CRC. |
Vincent Coubard |
638:c90ae1400bf2 | 147 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 148 | uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc); |
Vincent Coubard |
638:c90ae1400bf2 | 149 | |
Vincent Coubard |
638:c90ae1400bf2 | 150 | /**@brief Function for handling flashing module Radio Notification event. |
Vincent Coubard |
638:c90ae1400bf2 | 151 | * |
Vincent Coubard |
638:c90ae1400bf2 | 152 | * @note For flash writing to work safely while in a connection or while advertising, this function |
Vincent Coubard |
638:c90ae1400bf2 | 153 | * MUST be called from the Radio Notification module's event handler (see |
Vincent Coubard |
638:c90ae1400bf2 | 154 | * @ref ble_radio_notification for details). |
Vincent Coubard |
638:c90ae1400bf2 | 155 | * |
Vincent Coubard |
638:c90ae1400bf2 | 156 | * @param[in] radio_active TRUE if radio is active (or about to become active), FALSE otherwise. |
Vincent Coubard |
638:c90ae1400bf2 | 157 | */ |
Vincent Coubard |
638:c90ae1400bf2 | 158 | void ble_flash_on_radio_active_evt(bool radio_active); |
Vincent Coubard |
638:c90ae1400bf2 | 159 | |
Vincent Coubard |
638:c90ae1400bf2 | 160 | #endif // BLE_FLASH_H__ |
Vincent Coubard |
638:c90ae1400bf2 | 161 | |
Vincent Coubard |
638:c90ae1400bf2 | 162 | /** @} */ |