ble_flash.c 7.7 KB

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