ble_flash.h 5.6 KB

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