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