SG RFID nRF51822 fork

Fork of nRF51822 by Nordic Semiconductor

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 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  */
00012 
00013 /** @file
00014  *
00015  * @defgroup ble_flash_module Flash Manager
00016  * @{
00017  * @ingroup ble_sdk_lib
00018  * @brief Module for accessing flash memory.
00019  *
00020  * @details It contains functions for reading, writing and erasing one page in flash.
00021  *
00022  *          The module uses the first 32 bits of the flash page to write a magic number in order to
00023  *          determine if the page has been written or not.
00024  *
00025  * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the
00026  *       range 0 to 127), or in your application space! In both cases, this would end up
00027  *       with a hard fault.
00028  */
00029 
00030 #ifndef BLE_FLASH_H__
00031 #define BLE_FLASH_H__
00032 
00033 #include <stdint.h>
00034 #include <stdbool.h>
00035 #include <nrf51.h>
00036 
00037 #define BLE_FLASH_PAGE_SIZE     ((uint16_t)NRF_FICR->CODEPAGESIZE)  /**< Size of one flash page. */
00038 #define BLE_FLASH_MAGIC_NUMBER  0x45DE0000                          /**< Magic value to identify if flash contains valid data. */
00039 #define BLE_FLASH_EMPTY_MASK    0xFFFFFFFF                          /**< Bit mask that defines an empty address in flash. */
00040 
00041 
00042 /**@brief Macro for getting the end of the flash available for application.
00043  * 
00044  * @details    The result flash page number indicates the end boundary of the flash available 
00045  *             to the application. If a bootloader is used, the end will be the start of the 
00046  *             bootloader region. Otherwise, the end will be the size of the flash. 
00047  */
00048 #define BLE_FLASH_PAGE_END \
00049     ((NRF_UICR->BOOTLOADERADDR != BLE_FLASH_EMPTY_MASK) \
00050         ? (NRF_UICR->BOOTLOADERADDR / BLE_FLASH_PAGE_SIZE) \
00051         : NRF_FICR->CODESIZE)
00052 
00053 /**@brief Function for erasing the specified flash page, and then writes the given data to this page.
00054  *
00055  * @warning This operation blocks the CPU. DO NOT use while in a connection!
00056  *
00057  * @param[in]  page_num     Page number to update.
00058  * @param[in]  p_in_array   Pointer to a RAM area containing the elements to write in flash.
00059  *                          This area has to be 32 bits aligned.
00060  * @param[in]  word_count   Number of 32 bits words to write in flash.
00061  *
00062  * @return     NRF_SUCCESS on successful flash write, otherwise an error code.
00063  */
00064 uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count);
00065 
00066 /**@brief Function for reading data from flash to RAM.
00067  *
00068  * @param[in]  page_num       Page number to read.
00069  * @param[out] p_out_array    Pointer to a RAM area where the found data will be written. 
00070  *                            This area has to be 32 bits aligned.
00071  * @param[out] p_word_count   Number of 32 bits words read.
00072  *
00073  * @return     NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found
00074  *             in flash (first 32 bits not equal to the MAGIC_NUMBER+CRC).
00075  */
00076 uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count);
00077 
00078 /**@brief Function for erasing a flash page.
00079  *
00080  * @note This operation blocks the CPU, so it should not be done while the radio is running!
00081  *
00082  * @param[in]  page_num   Page number to erase.
00083  *
00084  * @return     NRF_SUCCESS on success, an error_code otherwise.
00085  */
00086 uint32_t ble_flash_page_erase(uint8_t page_num);
00087 
00088 /**@brief Function for writing one word to flash.
00089  *
00090  * @note Flash location to be written must have been erased previously.
00091  *
00092  * @param[in]  p_address   Pointer to flash location to be written.
00093  * @param[in]  value       Value to write to flash.
00094  *
00095  * @return     NRF_SUCCESS.
00096  */
00097 uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value);
00098 
00099 /**@brief Function for writing a data block to flash.
00100  *
00101  * @note Flash locations to be written must have been erased previously.
00102  *
00103  * @param[in]  p_address    Pointer to start of flash location to be written.
00104  * @param[in]  p_in_array   Pointer to start of flash block to be written.
00105  * @param[in]  word_count   Number of words to be written.
00106  *
00107  * @return     NRF_SUCCESS.
00108  */
00109 uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count);
00110 
00111 /**@brief Function for computing pointer to start of specified flash page.
00112  *
00113  * @param[in]  page_num       Page number.
00114  * @param[out] pp_page_addr   Pointer to start of flash page.
00115  *
00116  * @return     NRF_SUCCESS.
00117  */
00118 uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr);
00119 
00120 /**@brief Function for calculating a 16 bit CRC using the CRC-16-CCITT scheme.
00121  * 
00122  * @param[in]  p_data   Pointer to data on which the CRC is to be calulated.
00123  * @param[in]  size     Number of bytes on which the CRC is to be calulated.
00124  * @param[in]  p_crc    Initial CRC value (if NULL, a preset value is used as the initial value).
00125  *
00126  * @return     Calculated CRC.
00127  */
00128 uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc);
00129 
00130 /**@brief Function for handling flashing module Radio Notification event.
00131  *
00132  * @note For flash writing to work safely while in a connection or while advertising, this function
00133  *       MUST be called from the Radio Notification module's event handler (see
00134  *       @ref ble_radio_notification for details).
00135  *
00136  * @param[in]  radio_active   TRUE if radio is active (or about to become active), FALSE otherwise.
00137  */
00138 void ble_flash_on_radio_active_evt(bool radio_active);
00139 
00140 #endif // BLE_FLASH_H__
00141 
00142 /** @} */