nrf_fstorage_nvmc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_FSTORAGE)
  42. #include "nrf_fstorage_nvmc.h"
  43. #include <stdint.h>
  44. #include <string.h>
  45. #include <stdbool.h>
  46. #include "nrf_nvmc.h"
  47. #include "nrf_atomic.h"
  48. static nrf_fstorage_info_t m_flash_info =
  49. {
  50. #if defined(NRF51)
  51. .erase_unit = 1024,
  52. #elif defined(NRF52_SERIES)
  53. .erase_unit = 4096,
  54. #endif
  55. .program_unit = 4,
  56. .rmap = true,
  57. .wmap = false,
  58. };
  59. /* An operation initiated by fstorage is ongoing. */
  60. static nrf_atomic_flag_t m_flash_operation_ongoing;
  61. /* Send event to the event handler. */
  62. static void event_send(nrf_fstorage_t const * p_fs,
  63. nrf_fstorage_evt_id_t evt_id,
  64. void const * p_src,
  65. uint32_t addr,
  66. uint32_t len,
  67. void * p_param)
  68. {
  69. if (p_fs->evt_handler == NULL)
  70. {
  71. /* Nothing to do. */
  72. return;
  73. }
  74. nrf_fstorage_evt_t evt =
  75. {
  76. .result = NRF_SUCCESS,
  77. .id = evt_id,
  78. .addr = addr,
  79. .p_src = p_src,
  80. .len = len,
  81. .p_param = p_param,
  82. };
  83. p_fs->evt_handler(&evt);
  84. }
  85. static ret_code_t init(nrf_fstorage_t * p_fs, void * p_param)
  86. {
  87. UNUSED_PARAMETER(p_param);
  88. p_fs->p_flash_info = &m_flash_info;
  89. return NRF_SUCCESS;
  90. }
  91. static ret_code_t uninit(nrf_fstorage_t * p_fs, void * p_param)
  92. {
  93. UNUSED_PARAMETER(p_fs);
  94. UNUSED_PARAMETER(p_param);
  95. (void) nrf_atomic_flag_clear(&m_flash_operation_ongoing);
  96. return NRF_SUCCESS;
  97. }
  98. static ret_code_t read(nrf_fstorage_t const * p_fs, uint32_t src, void * p_dest, uint32_t len)
  99. {
  100. UNUSED_PARAMETER(p_fs);
  101. memcpy(p_dest, (uint32_t*)src, len);
  102. return NRF_SUCCESS;
  103. }
  104. static ret_code_t write(nrf_fstorage_t const * p_fs,
  105. uint32_t dest,
  106. void const * p_src,
  107. uint32_t len,
  108. void * p_param)
  109. {
  110. if (nrf_atomic_flag_set_fetch(&m_flash_operation_ongoing))
  111. {
  112. return NRF_ERROR_BUSY;
  113. }
  114. nrf_nvmc_write_words(dest, (uint32_t*)p_src, (len / m_flash_info.program_unit));
  115. /* Clear the flag before sending the event, to allow API calls in the event context. */
  116. (void) nrf_atomic_flag_clear(&m_flash_operation_ongoing);
  117. event_send(p_fs, NRF_FSTORAGE_EVT_WRITE_RESULT, p_src, dest, len, p_param);
  118. return NRF_SUCCESS;
  119. }
  120. static ret_code_t erase(nrf_fstorage_t const * p_fs,
  121. uint32_t page_addr,
  122. uint32_t len,
  123. void * p_param)
  124. {
  125. uint32_t progress = 0;
  126. if (nrf_atomic_flag_set_fetch(&m_flash_operation_ongoing))
  127. {
  128. return NRF_ERROR_BUSY;
  129. }
  130. while (progress != len)
  131. {
  132. nrf_nvmc_page_erase(page_addr + (progress * m_flash_info.erase_unit));
  133. progress++;
  134. }
  135. /* Clear the flag before sending the event, to allow API calls in the event context. */
  136. (void) nrf_atomic_flag_clear(&m_flash_operation_ongoing);
  137. event_send(p_fs, NRF_FSTORAGE_EVT_ERASE_RESULT, NULL, page_addr, len, p_param);
  138. return NRF_SUCCESS;
  139. }
  140. static uint8_t const * rmap(nrf_fstorage_t const * p_fs, uint32_t addr)
  141. {
  142. UNUSED_PARAMETER(p_fs);
  143. return (uint8_t*)addr;
  144. }
  145. static uint8_t * wmap(nrf_fstorage_t const * p_fs, uint32_t addr)
  146. {
  147. UNUSED_PARAMETER(p_fs);
  148. UNUSED_PARAMETER(addr);
  149. /* Not supported. */
  150. return NULL;
  151. }
  152. static bool is_busy(nrf_fstorage_t const * p_fs)
  153. {
  154. UNUSED_PARAMETER(p_fs);
  155. return m_flash_operation_ongoing;
  156. }
  157. /* The exported API. */
  158. nrf_fstorage_api_t nrf_fstorage_nvmc =
  159. {
  160. .init = init,
  161. .uninit = uninit,
  162. .read = read,
  163. .write = write,
  164. .erase = erase,
  165. .rmap = rmap,
  166. .wmap = wmap,
  167. .is_busy = is_busy
  168. };
  169. #endif // NRF_FSTORAGE_ENABLED