Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:56 2016 +0100
Revision:
28:041dac1366b2
Parent:
20:a90c48eb1d30
Child:
29:286940b7ee5a
Synchronized with git rev 012b8118
Author: Liyou Zhou
Pull in files from sdk 10.0

Who changed what in which revision?

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