Lancaster University fork of the Nordic nrf51-SDK repository, which actually lives on github: https://github.com/lancaster-university/nrf51-sdk

Dependents:   nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ble_flash.h Source File

ble_flash.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 /** @file
00034  *
00035  * @defgroup ble_flash_module Flash Manager
00036  * @{
00037  * @ingroup ble_sdk_lib
00038  * @brief Module for accessing flash memory.
00039  *
00040  * @details It contains functions for reading, writing and erasing one page in flash.
00041  *
00042  *          The module uses the first 32 bits of the flash page to write a magic number in order to
00043  *          determine if the page has been written or not.
00044  *
00045  * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the
00046  *       range 0 to 127), or in your application space! In both cases, this would end up
00047  *       with a hard fault.
00048  */
00049 
00050 #ifndef BLE_FLASH_H__
00051 #define BLE_FLASH_H__
00052 
00053 #include <stdint.h>
00054 #include <stdbool.h>
00055 #include "nrf.h"
00056 
00057 #define BLE_FLASH_PAGE_SIZE     ((uint16_t)NRF_FICR->CODEPAGESIZE)  /**< Size of one flash page. */
00058 #define BLE_FLASH_MAGIC_NUMBER  0x45DE0000                          /**< Magic value to identify if flash contains valid data. */
00059 #define BLE_FLASH_EMPTY_MASK    0xFFFFFFFF                          /**< Bit mask that defines an empty address in flash. */
00060 
00061 
00062 /**@brief Macro for getting the end of the flash available for application.
00063  * 
00064  * @details    The result flash page number indicates the end boundary of the flash available 
00065  *             to the application. If a bootloader is used, the end will be the start of the 
00066  *             bootloader region. Otherwise, the end will be the size of the flash. 
00067  */
00068 #define BLE_FLASH_PAGE_END \
00069     ((NRF_UICR->BOOTLOADERADDR != BLE_FLASH_EMPTY_MASK) \
00070         ? (NRF_UICR->BOOTLOADERADDR / BLE_FLASH_PAGE_SIZE) \
00071         : NRF_FICR->CODESIZE)
00072 
00073 /**@brief Function for erasing the specified flash page, and then writes the given data to this page.
00074  *
00075  * @warning This operation blocks the CPU. DO NOT use while in a connection!
00076  *
00077  * @param[in]  page_num     Page number to update.
00078  * @param[in]  p_in_array   Pointer to a RAM area containing the elements to write in flash.
00079  *                          This area has to be 32 bits aligned.
00080  * @param[in]  word_count   Number of 32 bits words to write in flash.
00081  *
00082  * @return     NRF_SUCCESS on successful flash write, otherwise an error code.
00083  */
00084 uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count);
00085 
00086 /**@brief Function for reading data from flash to RAM.
00087  *
00088  * @param[in]  page_num       Page number to read.
00089  * @param[out] p_out_array    Pointer to a RAM area where the found data will be written. 
00090  *                            This area has to be 32 bits aligned.
00091  * @param[out] p_word_count   Number of 32 bits words read.
00092  *
00093  * @return     NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found
00094  *             in flash (first 32 bits not equal to the MAGIC_NUMBER+CRC).
00095  */
00096 uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count);
00097 
00098 /**@brief Function for erasing a flash page.
00099  *
00100  * @note This operation blocks the CPU, so it should not be done while the radio is running!
00101  *
00102  * @param[in]  page_num   Page number to erase.
00103  *
00104  * @return     NRF_SUCCESS on success, an error_code otherwise.
00105  */
00106 uint32_t ble_flash_page_erase(uint8_t page_num);
00107 
00108 /**@brief Function for writing one word to flash.
00109  *
00110  * @note Flash location to be written must have been erased previously.
00111  *
00112  * @param[in]  p_address   Pointer to flash location to be written.
00113  * @param[in]  value       Value to write to flash.
00114  *
00115  * @return     NRF_SUCCESS.
00116  */
00117 uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value);
00118 
00119 /**@brief Function for writing a data block to flash.
00120  *
00121  * @note Flash locations to be written must have been erased previously.
00122  *
00123  * @param[in]  p_address    Pointer to start of flash location to be written.
00124  * @param[in]  p_in_array   Pointer to start of flash block to be written.
00125  * @param[in]  word_count   Number of words to be written.
00126  *
00127  * @return     NRF_SUCCESS.
00128  */
00129 uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count);
00130 
00131 /**@brief Function for computing pointer to start of specified flash page.
00132  *
00133  * @param[in]  page_num       Page number.
00134  * @param[out] pp_page_addr   Pointer to start of flash page.
00135  *
00136  * @return     NRF_SUCCESS.
00137  */
00138 uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr);
00139 
00140 /**@brief Function for calculating a 16 bit CRC using the CRC-16-CCITT scheme.
00141  * 
00142  * @param[in]  p_data   Pointer to data on which the CRC is to be calulated.
00143  * @param[in]  size     Number of bytes on which the CRC is to be calulated.
00144  * @param[in]  p_crc    Initial CRC value (if NULL, a preset value is used as the initial value).
00145  *
00146  * @return     Calculated CRC.
00147  */
00148 uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc);
00149 
00150 /**@brief Function for handling flashing module Radio Notification event.
00151  *
00152  * @note For flash writing to work safely while in a connection or while advertising, this function
00153  *       MUST be called from the Radio Notification module's event handler (see
00154  *       @ref ble_radio_notification for details).
00155  *
00156  * @param[in]  radio_active   TRUE if radio is active (or about to become active), FALSE otherwise.
00157  */
00158 void ble_flash_on_radio_active_evt(bool radio_active);
00159 
00160 #endif // BLE_FLASH_H__
00161 
00162 /** @} */