project for nrf51822 qfab
Dependencies: eddystone_URL mbed
Fork of eddystone_URL by
nRF51822/nordic/nrf-sdk/ble/ble_flash.h@0:76dfa9657d9d, 2014-11-12 (annotated)
- Committer:
- jksoft
- Date:
- Wed Nov 12 02:40:34 2014 +0000
- Revision:
- 0:76dfa9657d9d
????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:76dfa9657d9d | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
jksoft | 0:76dfa9657d9d | 2 | * |
jksoft | 0:76dfa9657d9d | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
jksoft | 0:76dfa9657d9d | 4 | * Terms and conditions of usage are described in detail in NORDIC |
jksoft | 0:76dfa9657d9d | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
jksoft | 0:76dfa9657d9d | 6 | * |
jksoft | 0:76dfa9657d9d | 7 | * Licensees are granted free, non-transferable use of the information. NO |
jksoft | 0:76dfa9657d9d | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
jksoft | 0:76dfa9657d9d | 9 | * the file. |
jksoft | 0:76dfa9657d9d | 10 | * |
jksoft | 0:76dfa9657d9d | 11 | */ |
jksoft | 0:76dfa9657d9d | 12 | |
jksoft | 0:76dfa9657d9d | 13 | /** @file |
jksoft | 0:76dfa9657d9d | 14 | * |
jksoft | 0:76dfa9657d9d | 15 | * @defgroup ble_flash_module Flash Manager |
jksoft | 0:76dfa9657d9d | 16 | * @{ |
jksoft | 0:76dfa9657d9d | 17 | * @ingroup ble_sdk_lib |
jksoft | 0:76dfa9657d9d | 18 | * @brief Module for accessing flash memory. |
jksoft | 0:76dfa9657d9d | 19 | * |
jksoft | 0:76dfa9657d9d | 20 | * @details It contains functions for reading, writing and erasing one page in flash. |
jksoft | 0:76dfa9657d9d | 21 | * |
jksoft | 0:76dfa9657d9d | 22 | * The module uses the first 32 bits of the flash page to write a magic number in order to |
jksoft | 0:76dfa9657d9d | 23 | * determine if the page has been written or not. |
jksoft | 0:76dfa9657d9d | 24 | * |
jksoft | 0:76dfa9657d9d | 25 | * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the |
jksoft | 0:76dfa9657d9d | 26 | * range 0 to 127), or in your application space! In both cases, this would end up |
jksoft | 0:76dfa9657d9d | 27 | * with a hard fault. |
jksoft | 0:76dfa9657d9d | 28 | */ |
jksoft | 0:76dfa9657d9d | 29 | |
jksoft | 0:76dfa9657d9d | 30 | #ifndef BLE_FLASH_H__ |
jksoft | 0:76dfa9657d9d | 31 | #define BLE_FLASH_H__ |
jksoft | 0:76dfa9657d9d | 32 | |
jksoft | 0:76dfa9657d9d | 33 | #include <stdint.h> |
jksoft | 0:76dfa9657d9d | 34 | #include <stdbool.h> |
jksoft | 0:76dfa9657d9d | 35 | #include <nrf51.h> |
jksoft | 0:76dfa9657d9d | 36 | |
jksoft | 0:76dfa9657d9d | 37 | #define BLE_FLASH_PAGE_SIZE ((uint16_t)NRF_FICR->CODEPAGESIZE) /**< Size of one flash page. */ |
jksoft | 0:76dfa9657d9d | 38 | #define BLE_FLASH_MAGIC_NUMBER 0x45DE0000 /**< Magic value to identify if flash contains valid data. */ |
jksoft | 0:76dfa9657d9d | 39 | #define BLE_FLASH_EMPTY_MASK 0xFFFFFFFF /**< Bit mask that defines an empty address in flash. */ |
jksoft | 0:76dfa9657d9d | 40 | |
jksoft | 0:76dfa9657d9d | 41 | |
jksoft | 0:76dfa9657d9d | 42 | /**@brief Macro for getting the end of the flash available for application. |
jksoft | 0:76dfa9657d9d | 43 | * |
jksoft | 0:76dfa9657d9d | 44 | * @details The result flash page number indicates the end boundary of the flash available |
jksoft | 0:76dfa9657d9d | 45 | * to the application. If a bootloader is used, the end will be the start of the |
jksoft | 0:76dfa9657d9d | 46 | * bootloader region. Otherwise, the end will be the size of the flash. |
jksoft | 0:76dfa9657d9d | 47 | */ |
jksoft | 0:76dfa9657d9d | 48 | #define BLE_FLASH_PAGE_END \ |
jksoft | 0:76dfa9657d9d | 49 | ((NRF_UICR->BOOTLOADERADDR != BLE_FLASH_EMPTY_MASK) \ |
jksoft | 0:76dfa9657d9d | 50 | ? (NRF_UICR->BOOTLOADERADDR / BLE_FLASH_PAGE_SIZE) \ |
jksoft | 0:76dfa9657d9d | 51 | : NRF_FICR->CODESIZE) |
jksoft | 0:76dfa9657d9d | 52 | |
jksoft | 0:76dfa9657d9d | 53 | /**@brief Function for erasing the specified flash page, and then writes the given data to this page. |
jksoft | 0:76dfa9657d9d | 54 | * |
jksoft | 0:76dfa9657d9d | 55 | * @warning This operation blocks the CPU. DO NOT use while in a connection! |
jksoft | 0:76dfa9657d9d | 56 | * |
jksoft | 0:76dfa9657d9d | 57 | * @param[in] page_num Page number to update. |
jksoft | 0:76dfa9657d9d | 58 | * @param[in] p_in_array Pointer to a RAM area containing the elements to write in flash. |
jksoft | 0:76dfa9657d9d | 59 | * This area has to be 32 bits aligned. |
jksoft | 0:76dfa9657d9d | 60 | * @param[in] word_count Number of 32 bits words to write in flash. |
jksoft | 0:76dfa9657d9d | 61 | * |
jksoft | 0:76dfa9657d9d | 62 | * @return NRF_SUCCESS on successful flash write, otherwise an error code. |
jksoft | 0:76dfa9657d9d | 63 | */ |
jksoft | 0:76dfa9657d9d | 64 | uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count); |
jksoft | 0:76dfa9657d9d | 65 | |
jksoft | 0:76dfa9657d9d | 66 | /**@brief Function for reading data from flash to RAM. |
jksoft | 0:76dfa9657d9d | 67 | * |
jksoft | 0:76dfa9657d9d | 68 | * @param[in] page_num Page number to read. |
jksoft | 0:76dfa9657d9d | 69 | * @param[out] p_out_array Pointer to a RAM area where the found data will be written. |
jksoft | 0:76dfa9657d9d | 70 | * This area has to be 32 bits aligned. |
jksoft | 0:76dfa9657d9d | 71 | * @param[out] p_word_count Number of 32 bits words read. |
jksoft | 0:76dfa9657d9d | 72 | * |
jksoft | 0:76dfa9657d9d | 73 | * @return NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found |
jksoft | 0:76dfa9657d9d | 74 | * in flash (first 32 bits not equal to the MAGIC_NUMBER+CRC). |
jksoft | 0:76dfa9657d9d | 75 | */ |
jksoft | 0:76dfa9657d9d | 76 | uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count); |
jksoft | 0:76dfa9657d9d | 77 | |
jksoft | 0:76dfa9657d9d | 78 | /**@brief Function for erasing a flash page. |
jksoft | 0:76dfa9657d9d | 79 | * |
jksoft | 0:76dfa9657d9d | 80 | * @note This operation blocks the CPU, so it should not be done while the radio is running! |
jksoft | 0:76dfa9657d9d | 81 | * |
jksoft | 0:76dfa9657d9d | 82 | * @param[in] page_num Page number to erase. |
jksoft | 0:76dfa9657d9d | 83 | * |
jksoft | 0:76dfa9657d9d | 84 | * @return NRF_SUCCESS on success, an error_code otherwise. |
jksoft | 0:76dfa9657d9d | 85 | */ |
jksoft | 0:76dfa9657d9d | 86 | uint32_t ble_flash_page_erase(uint8_t page_num); |
jksoft | 0:76dfa9657d9d | 87 | |
jksoft | 0:76dfa9657d9d | 88 | /**@brief Function for writing one word to flash. |
jksoft | 0:76dfa9657d9d | 89 | * |
jksoft | 0:76dfa9657d9d | 90 | * @note Flash location to be written must have been erased previously. |
jksoft | 0:76dfa9657d9d | 91 | * |
jksoft | 0:76dfa9657d9d | 92 | * @param[in] p_address Pointer to flash location to be written. |
jksoft | 0:76dfa9657d9d | 93 | * @param[in] value Value to write to flash. |
jksoft | 0:76dfa9657d9d | 94 | * |
jksoft | 0:76dfa9657d9d | 95 | * @return NRF_SUCCESS. |
jksoft | 0:76dfa9657d9d | 96 | */ |
jksoft | 0:76dfa9657d9d | 97 | uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value); |
jksoft | 0:76dfa9657d9d | 98 | |
jksoft | 0:76dfa9657d9d | 99 | /**@brief Function for writing a data block to flash. |
jksoft | 0:76dfa9657d9d | 100 | * |
jksoft | 0:76dfa9657d9d | 101 | * @note Flash locations to be written must have been erased previously. |
jksoft | 0:76dfa9657d9d | 102 | * |
jksoft | 0:76dfa9657d9d | 103 | * @param[in] p_address Pointer to start of flash location to be written. |
jksoft | 0:76dfa9657d9d | 104 | * @param[in] p_in_array Pointer to start of flash block to be written. |
jksoft | 0:76dfa9657d9d | 105 | * @param[in] word_count Number of words to be written. |
jksoft | 0:76dfa9657d9d | 106 | * |
jksoft | 0:76dfa9657d9d | 107 | * @return NRF_SUCCESS. |
jksoft | 0:76dfa9657d9d | 108 | */ |
jksoft | 0:76dfa9657d9d | 109 | uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count); |
jksoft | 0:76dfa9657d9d | 110 | |
jksoft | 0:76dfa9657d9d | 111 | /**@brief Function for computing pointer to start of specified flash page. |
jksoft | 0:76dfa9657d9d | 112 | * |
jksoft | 0:76dfa9657d9d | 113 | * @param[in] page_num Page number. |
jksoft | 0:76dfa9657d9d | 114 | * @param[out] pp_page_addr Pointer to start of flash page. |
jksoft | 0:76dfa9657d9d | 115 | * |
jksoft | 0:76dfa9657d9d | 116 | * @return NRF_SUCCESS. |
jksoft | 0:76dfa9657d9d | 117 | */ |
jksoft | 0:76dfa9657d9d | 118 | uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr); |
jksoft | 0:76dfa9657d9d | 119 | |
jksoft | 0:76dfa9657d9d | 120 | /**@brief Function for calculating a 16 bit CRC using the CRC-16-CCITT scheme. |
jksoft | 0:76dfa9657d9d | 121 | * |
jksoft | 0:76dfa9657d9d | 122 | * @param[in] p_data Pointer to data on which the CRC is to be calulated. |
jksoft | 0:76dfa9657d9d | 123 | * @param[in] size Number of bytes on which the CRC is to be calulated. |
jksoft | 0:76dfa9657d9d | 124 | * @param[in] p_crc Initial CRC value (if NULL, a preset value is used as the initial value). |
jksoft | 0:76dfa9657d9d | 125 | * |
jksoft | 0:76dfa9657d9d | 126 | * @return Calculated CRC. |
jksoft | 0:76dfa9657d9d | 127 | */ |
jksoft | 0:76dfa9657d9d | 128 | uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc); |
jksoft | 0:76dfa9657d9d | 129 | |
jksoft | 0:76dfa9657d9d | 130 | /**@brief Function for handling flashing module Radio Notification event. |
jksoft | 0:76dfa9657d9d | 131 | * |
jksoft | 0:76dfa9657d9d | 132 | * @note For flash writing to work safely while in a connection or while advertising, this function |
jksoft | 0:76dfa9657d9d | 133 | * MUST be called from the Radio Notification module's event handler (see |
jksoft | 0:76dfa9657d9d | 134 | * @ref ble_radio_notification for details). |
jksoft | 0:76dfa9657d9d | 135 | * |
jksoft | 0:76dfa9657d9d | 136 | * @param[in] radio_active TRUE if radio is active (or about to become active), FALSE otherwise. |
jksoft | 0:76dfa9657d9d | 137 | */ |
jksoft | 0:76dfa9657d9d | 138 | void ble_flash_on_radio_active_evt(bool radio_active); |
jksoft | 0:76dfa9657d9d | 139 | |
jksoft | 0:76dfa9657d9d | 140 | #endif // BLE_FLASH_H__ |
jksoft | 0:76dfa9657d9d | 141 | |
jksoft | 0:76dfa9657d9d | 142 | /** @} */ |