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

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:40 2016 +0100
Revision:
19:47192cb9def7
Parent:
10:233fefd8162b
Child:
20:a90c48eb1d30
Synchronized with git rev 9251259f
Author: Liyou Zhou
Copy over coresponding files from nordic-sdk 9.0.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 19:47192cb9def7 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
vcoubard 19:47192cb9def7 2 *
vcoubard 19:47192cb9def7 3 * The information contained herein is property of Nordic Semiconductor ASA.
vcoubard 19:47192cb9def7 4 * Terms and conditions of usage are described in detail in NORDIC
vcoubard 19:47192cb9def7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
vcoubard 19:47192cb9def7 6 *
vcoubard 19:47192cb9def7 7 * Licensees are granted free, non-transferable use of the information. NO
vcoubard 19:47192cb9def7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
vcoubard 19:47192cb9def7 9 * the file.
vcoubard 19:47192cb9def7 10 *
Vincent Coubard 0:f2542974c862 11 */
Vincent Coubard 0:f2542974c862 12
Vincent Coubard 0:f2542974c862 13 #include "ble_flash.h"
Vincent Coubard 0:f2542974c862 14 #include <stdlib.h>
Vincent Coubard 0:f2542974c862 15 #include <stdint.h>
Vincent Coubard 0:f2542974c862 16 #include <string.h>
Vincent Coubard 0:f2542974c862 17 #include "nrf_soc.h"
Vincent Coubard 0:f2542974c862 18 #include "nordic_common.h"
Vincent Coubard 0:f2542974c862 19 #include "nrf_error.h"
Vincent Coubard 0:f2542974c862 20 #include "nrf.h"
Vincent Coubard 0:f2542974c862 21 #include "nrf51_bitfields.h"
Vincent Coubard 0:f2542974c862 22 #include "app_util.h"
Vincent Coubard 0:f2542974c862 23
Vincent Coubard 0:f2542974c862 24
Vincent Coubard 0:f2542974c862 25 static volatile bool m_radio_active = false; /**< TRUE if radio is active (or about to become active), FALSE otherwise. */
Vincent Coubard 0:f2542974c862 26
Vincent Coubard 0:f2542974c862 27
Vincent Coubard 0:f2542974c862 28 uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc)
Vincent Coubard 0:f2542974c862 29 {
Vincent Coubard 0:f2542974c862 30 uint16_t i;
Vincent Coubard 0:f2542974c862 31 uint16_t crc = (p_crc == NULL) ? 0xffff : *p_crc;
Vincent Coubard 0:f2542974c862 32
Vincent Coubard 0:f2542974c862 33 for (i = 0; i < size; i++)
Vincent Coubard 0:f2542974c862 34 {
Vincent Coubard 0:f2542974c862 35 crc = (unsigned char)(crc >> 8) | (crc << 8);
Vincent Coubard 0:f2542974c862 36 crc ^= p_data[i];
Vincent Coubard 0:f2542974c862 37 crc ^= (unsigned char)(crc & 0xff) >> 4;
Vincent Coubard 0:f2542974c862 38 crc ^= (crc << 8) << 4;
Vincent Coubard 0:f2542974c862 39 crc ^= ((crc & 0xff) << 4) << 1;
Vincent Coubard 0:f2542974c862 40 }
Vincent Coubard 0:f2542974c862 41 return crc;
Vincent Coubard 0:f2542974c862 42 }
Vincent Coubard 0:f2542974c862 43
Vincent Coubard 0:f2542974c862 44
Vincent Coubard 0:f2542974c862 45 /**@brief Function for erasing a page in flash.
Vincent Coubard 0:f2542974c862 46 *
Vincent Coubard 0:f2542974c862 47 * @param[in] p_page Pointer to first word in page to be erased.
Vincent Coubard 0:f2542974c862 48 */
Vincent Coubard 0:f2542974c862 49 static void flash_page_erase(uint32_t * p_page)
Vincent Coubard 0:f2542974c862 50 {
Vincent Coubard 0:f2542974c862 51 // Turn on flash erase enable and wait until the NVMC is ready.
Vincent Coubard 0:f2542974c862 52 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 53 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 54 {
Vincent Coubard 0:f2542974c862 55 // Do nothing.
Vincent Coubard 0:f2542974c862 56 }
Vincent Coubard 0:f2542974c862 57
Vincent Coubard 0:f2542974c862 58 // Erase page.
Vincent Coubard 0:f2542974c862 59 NRF_NVMC->ERASEPAGE = (uint32_t)p_page;
Vincent Coubard 0:f2542974c862 60 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 61 {
Vincent Coubard 0:f2542974c862 62 // Do nothing.
Vincent Coubard 0:f2542974c862 63 }
Vincent Coubard 0:f2542974c862 64
Vincent Coubard 0:f2542974c862 65 // Turn off flash erase enable and wait until the NVMC is ready.
Vincent Coubard 0:f2542974c862 66 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 67 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 68 {
Vincent Coubard 0:f2542974c862 69 // Do nothing
Vincent Coubard 0:f2542974c862 70 }
Vincent Coubard 0:f2542974c862 71 }
Vincent Coubard 0:f2542974c862 72
Vincent Coubard 0:f2542974c862 73
Vincent Coubard 0:f2542974c862 74 /**@brief Function for writing one word to flash. Unprotected write, which can interfere with radio communication.
Vincent Coubard 0:f2542974c862 75 *
Vincent Coubard 0:f2542974c862 76 * @details This function DOES NOT use the m_radio_active variable, but will force the write even
Vincent Coubard 0:f2542974c862 77 * when the radio is active. To be used only from @ref ble_flash_page_write.
Vincent Coubard 0:f2542974c862 78 *
Vincent Coubard 0:f2542974c862 79 * @note Flash location to be written must have been erased previously.
Vincent Coubard 0:f2542974c862 80 *
Vincent Coubard 0:f2542974c862 81 * @param[in] p_address Pointer to flash location to be written.
Vincent Coubard 0:f2542974c862 82 * @param[in] value Value to write to flash.
Vincent Coubard 0:f2542974c862 83 */
Vincent Coubard 0:f2542974c862 84 static void flash_word_unprotected_write(uint32_t * p_address, uint32_t value)
Vincent Coubard 0:f2542974c862 85 {
Vincent Coubard 0:f2542974c862 86 // Turn on flash write enable and wait until the NVMC is ready.
Vincent Coubard 0:f2542974c862 87 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 88 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 89 {
Vincent Coubard 0:f2542974c862 90 // Do nothing.
Vincent Coubard 0:f2542974c862 91 }
Vincent Coubard 0:f2542974c862 92 *p_address = value;
Vincent Coubard 0:f2542974c862 93
Vincent Coubard 0:f2542974c862 94 // Wait flash write to finish
Vincent Coubard 0:f2542974c862 95 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 96 {
Vincent Coubard 0:f2542974c862 97 // Do nothing.
Vincent Coubard 0:f2542974c862 98 }
Vincent Coubard 0:f2542974c862 99
Vincent Coubard 0:f2542974c862 100 // Turn off flash write enable and wait until the NVMC is ready.
Vincent Coubard 0:f2542974c862 101 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 102 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 103 {
Vincent Coubard 0:f2542974c862 104 // Do nothing.
Vincent Coubard 0:f2542974c862 105 }
Vincent Coubard 0:f2542974c862 106 }
Vincent Coubard 0:f2542974c862 107
Vincent Coubard 0:f2542974c862 108
Vincent Coubard 0:f2542974c862 109 /**@brief Function for writing one word to flash.
Vincent Coubard 0:f2542974c862 110 *
Vincent Coubard 0:f2542974c862 111 * @note Flash location to be written must have been erased previously.
Vincent Coubard 0:f2542974c862 112 *
Vincent Coubard 0:f2542974c862 113 * @param[in] p_address Pointer to flash location to be written.
Vincent Coubard 0:f2542974c862 114 * @param[in] value Value to write to flash.
Vincent Coubard 0:f2542974c862 115 */
Vincent Coubard 0:f2542974c862 116 static void flash_word_write(uint32_t * p_address, uint32_t value)
Vincent Coubard 0:f2542974c862 117 {
Vincent Coubard 0:f2542974c862 118 // If radio is active, wait for it to become inactive.
Vincent Coubard 0:f2542974c862 119 while (m_radio_active)
Vincent Coubard 0:f2542974c862 120 {
Vincent Coubard 0:f2542974c862 121 // Do nothing (just wait for radio to become inactive).
Vincent Coubard 0:f2542974c862 122 (void) sd_app_evt_wait();
Vincent Coubard 0:f2542974c862 123 }
Vincent Coubard 0:f2542974c862 124
Vincent Coubard 0:f2542974c862 125 // Turn on flash write enable and wait until the NVMC is ready.
Vincent Coubard 0:f2542974c862 126 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 127 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 128 {
Vincent Coubard 0:f2542974c862 129 // Do nothing.
Vincent Coubard 0:f2542974c862 130 }
Vincent Coubard 0:f2542974c862 131
Vincent Coubard 0:f2542974c862 132 *p_address = value;
Vincent Coubard 0:f2542974c862 133 // Wait flash write to finish
Vincent Coubard 0:f2542974c862 134 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 135 {
Vincent Coubard 0:f2542974c862 136 // Do nothing.
Vincent Coubard 0:f2542974c862 137 }
Vincent Coubard 0:f2542974c862 138 // Turn off flash write enable and wait until the NVMC is ready.
Vincent Coubard 0:f2542974c862 139 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 140 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 141 {
Vincent Coubard 0:f2542974c862 142 // Do nothing
Vincent Coubard 0:f2542974c862 143 }
Vincent Coubard 0:f2542974c862 144 }
Vincent Coubard 0:f2542974c862 145
Vincent Coubard 0:f2542974c862 146
Vincent Coubard 0:f2542974c862 147 uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value)
Vincent Coubard 0:f2542974c862 148 {
Vincent Coubard 0:f2542974c862 149 flash_word_write(p_address, value);
Vincent Coubard 0:f2542974c862 150 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 151 }
Vincent Coubard 0:f2542974c862 152
Vincent Coubard 0:f2542974c862 153
Vincent Coubard 0:f2542974c862 154 uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count)
Vincent Coubard 0:f2542974c862 155 {
Vincent Coubard 0:f2542974c862 156 uint16_t i;
Vincent Coubard 0:f2542974c862 157
Vincent Coubard 0:f2542974c862 158 for (i = 0; i < word_count; i++)
Vincent Coubard 0:f2542974c862 159 {
Vincent Coubard 0:f2542974c862 160 flash_word_write(p_address, p_in_array[i]);
Vincent Coubard 0:f2542974c862 161 p_address++;
Vincent Coubard 0:f2542974c862 162 }
Vincent Coubard 0:f2542974c862 163
Vincent Coubard 0:f2542974c862 164 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 165 }
Vincent Coubard 0:f2542974c862 166
Vincent Coubard 0:f2542974c862 167
Vincent Coubard 0:f2542974c862 168 uint32_t ble_flash_page_erase(uint8_t page_num)
Vincent Coubard 0:f2542974c862 169 {
Vincent Coubard 0:f2542974c862 170 uint32_t * p_page = (uint32_t *)(BLE_FLASH_PAGE_SIZE * page_num);
Vincent Coubard 0:f2542974c862 171 flash_page_erase(p_page);
Vincent Coubard 0:f2542974c862 172
Vincent Coubard 0:f2542974c862 173 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 174 }
Vincent Coubard 0:f2542974c862 175
Vincent Coubard 0:f2542974c862 176
Vincent Coubard 0:f2542974c862 177 uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count)
Vincent Coubard 0:f2542974c862 178 {
Vincent Coubard 0:f2542974c862 179 int i;
Vincent Coubard 0:f2542974c862 180 uint32_t * p_page;
Vincent Coubard 0:f2542974c862 181 uint32_t * p_curr_addr;
Vincent Coubard 0:f2542974c862 182 uint16_t in_data_crc;
Vincent Coubard 0:f2542974c862 183 uint16_t flash_crc;
Vincent Coubard 0:f2542974c862 184 uint32_t flash_header;
Vincent Coubard 0:f2542974c862 185
Vincent Coubard 0:f2542974c862 186 p_page = (uint32_t *)(BLE_FLASH_PAGE_SIZE * page_num);
Vincent Coubard 0:f2542974c862 187 p_curr_addr = p_page;
Vincent Coubard 0:f2542974c862 188
Vincent Coubard 0:f2542974c862 189 // Calculate CRC of the data to write.
Vincent Coubard 0:f2542974c862 190 in_data_crc = ble_flash_crc16_compute((uint8_t *)p_in_array,
Vincent Coubard 0:f2542974c862 191 word_count * sizeof(uint32_t),
Vincent Coubard 0:f2542974c862 192 NULL);
Vincent Coubard 0:f2542974c862 193
Vincent Coubard 0:f2542974c862 194 // Compare the calculated to the one in flash.
Vincent Coubard 0:f2542974c862 195 flash_header = *p_curr_addr;
Vincent Coubard 0:f2542974c862 196 flash_crc = (uint16_t)flash_header;
Vincent Coubard 0:f2542974c862 197
Vincent Coubard 0:f2542974c862 198 if (flash_crc == in_data_crc)
Vincent Coubard 0:f2542974c862 199 {
Vincent Coubard 0:f2542974c862 200 // Data is the same as the data already stored in flash, return without modifying flash.
Vincent Coubard 0:f2542974c862 201 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 202 }
Vincent Coubard 0:f2542974c862 203
Vincent Coubard 0:f2542974c862 204 // Erase flash page
Vincent Coubard 0:f2542974c862 205 flash_page_erase(p_page);
Vincent Coubard 0:f2542974c862 206
Vincent Coubard 0:f2542974c862 207 // Reserve space for magic number (for detecting if flash content is valid).
Vincent Coubard 0:f2542974c862 208 p_curr_addr++;
Vincent Coubard 0:f2542974c862 209
Vincent Coubard 0:f2542974c862 210 // Reserve space for saving word_count.
Vincent Coubard 0:f2542974c862 211 p_curr_addr++;
Vincent Coubard 0:f2542974c862 212
Vincent Coubard 0:f2542974c862 213 // Write data
Vincent Coubard 0:f2542974c862 214 for (i = 0; i < word_count; i++)
Vincent Coubard 0:f2542974c862 215 {
Vincent Coubard 0:f2542974c862 216 flash_word_unprotected_write(p_curr_addr, p_in_array[i]);
Vincent Coubard 0:f2542974c862 217 p_curr_addr++;
Vincent Coubard 0:f2542974c862 218 }
Vincent Coubard 0:f2542974c862 219
Vincent Coubard 0:f2542974c862 220 // Write number of elements.
Vincent Coubard 0:f2542974c862 221 flash_word_write(p_page + 1, (uint32_t)(word_count));
Vincent Coubard 0:f2542974c862 222
Vincent Coubard 0:f2542974c862 223 // Write magic number and CRC to indicate that flash content is valid.
Vincent Coubard 0:f2542974c862 224 flash_header = BLE_FLASH_MAGIC_NUMBER | (uint32_t)in_data_crc;
Vincent Coubard 0:f2542974c862 225 flash_word_write(p_page, flash_header);
Vincent Coubard 0:f2542974c862 226
Vincent Coubard 0:f2542974c862 227 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 228 }
Vincent Coubard 0:f2542974c862 229
Vincent Coubard 0:f2542974c862 230
Vincent Coubard 0:f2542974c862 231 uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count)
Vincent Coubard 0:f2542974c862 232 {
Vincent Coubard 0:f2542974c862 233 int byte_count;
Vincent Coubard 0:f2542974c862 234 uint32_t * p_page;
Vincent Coubard 0:f2542974c862 235 uint32_t * p_curr_addr;
Vincent Coubard 0:f2542974c862 236 uint32_t flash_header;
Vincent Coubard 0:f2542974c862 237 uint32_t calc_header;
Vincent Coubard 0:f2542974c862 238 uint16_t calc_crc;
Vincent Coubard 0:f2542974c862 239 uint32_t tmp;
Vincent Coubard 0:f2542974c862 240
Vincent Coubard 0:f2542974c862 241 p_page = (uint32_t *)(BLE_FLASH_PAGE_SIZE * page_num);
Vincent Coubard 0:f2542974c862 242 p_curr_addr = p_page;
Vincent Coubard 0:f2542974c862 243
Vincent Coubard 0:f2542974c862 244 // Check if block is valid
Vincent Coubard 0:f2542974c862 245 flash_header = *p_curr_addr;
Vincent Coubard 0:f2542974c862 246 tmp = flash_header & 0xFFFF0000;
Vincent Coubard 0:f2542974c862 247 if (tmp != BLE_FLASH_MAGIC_NUMBER)
Vincent Coubard 0:f2542974c862 248 {
Vincent Coubard 0:f2542974c862 249 *p_word_count = 0;
Vincent Coubard 0:f2542974c862 250 return NRF_ERROR_NOT_FOUND;
Vincent Coubard 0:f2542974c862 251 }
Vincent Coubard 0:f2542974c862 252 p_curr_addr++;
Vincent Coubard 0:f2542974c862 253
Vincent Coubard 0:f2542974c862 254 // Read number of elements
Vincent Coubard 0:f2542974c862 255 *p_word_count = (uint8_t)(*(p_curr_addr));
Vincent Coubard 0:f2542974c862 256 p_curr_addr++;
Vincent Coubard 0:f2542974c862 257
Vincent Coubard 0:f2542974c862 258 // Read data
Vincent Coubard 0:f2542974c862 259 byte_count = (*p_word_count) * sizeof(uint32_t);
Vincent Coubard 0:f2542974c862 260 memcpy(p_out_array, p_curr_addr, byte_count);
Vincent Coubard 0:f2542974c862 261
Vincent Coubard 0:f2542974c862 262 // Check CRC
Vincent Coubard 0:f2542974c862 263 calc_crc = ble_flash_crc16_compute((uint8_t *)p_out_array,
Vincent Coubard 0:f2542974c862 264 (*p_word_count) * sizeof(uint32_t),
Vincent Coubard 0:f2542974c862 265 NULL);
Vincent Coubard 0:f2542974c862 266 calc_header = BLE_FLASH_MAGIC_NUMBER | (uint32_t)calc_crc;
Vincent Coubard 0:f2542974c862 267
Vincent Coubard 0:f2542974c862 268 if (calc_header != flash_header)
Vincent Coubard 0:f2542974c862 269 {
Vincent Coubard 0:f2542974c862 270 return NRF_ERROR_NOT_FOUND;
Vincent Coubard 0:f2542974c862 271 }
Vincent Coubard 0:f2542974c862 272
Vincent Coubard 0:f2542974c862 273 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 274 }
Vincent Coubard 0:f2542974c862 275
Vincent Coubard 0:f2542974c862 276
Vincent Coubard 0:f2542974c862 277 uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr)
Vincent Coubard 0:f2542974c862 278 {
Vincent Coubard 0:f2542974c862 279 *pp_page_addr = (uint32_t *)(BLE_FLASH_PAGE_SIZE * page_num);
Vincent Coubard 0:f2542974c862 280 return NRF_SUCCESS;
Vincent Coubard 0:f2542974c862 281 }
Vincent Coubard 0:f2542974c862 282
Vincent Coubard 0:f2542974c862 283
Vincent Coubard 0:f2542974c862 284 void ble_flash_on_radio_active_evt(bool radio_active)
Vincent Coubard 0:f2542974c862 285 {
Vincent Coubard 0:f2542974c862 286 m_radio_active = radio_active;
vcoubard 1:ebc0e0ef0a11 287 }