Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Flash.h 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Flash.h - Flash library
  3. * Original Copyright (c) 2017 Frank Holtz. All right reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file Flash.h
  21. * @brief Flash abstraction layer
  22. *
  23. * @ingroup NVM
  24. * @details Nonvolatile Memory Class
  25. * @{
  26. */
  27. #pragma once
  28. #include <Arduino.h>
  29. #include <stdio.h> // for size_t
  30. /*
  31. * Define characteristics of Flash
  32. *
  33. * @def FLASH_ERASE_CYCLES
  34. * @brief Specified number of erase cycles
  35. *
  36. * @def FLASH_PAGE_SIZE
  37. * @brief Used/supported Flash page size
  38. *
  39. * @def FLASH_ERASE_PAGE_TIME
  40. * @brief Time in ms to delete a page
  41. *
  42. * @def FLASH_WRITES_PER_WORD
  43. * @brief How often a dataword (32 bit) can be written
  44. *
  45. * @def FLASH_WRITES_PER_PAGE
  46. * @brief How many writes are allowed into a page
  47. *
  48. * @def FLASH_SUPPORTS_RANDOM_WRITE
  49. * @brief Set this if it is allowed to write to a page in random order.
  50. */
  51. #if defined(NRF51)
  52. #define FLASH_ERASE_CYCLES 20000
  53. #define FLASH_PAGE_SIZE 1024
  54. #define FLASH_ERASE_PAGE_TIME 23
  55. #define FLASH_SUPPORTS_RANDOM_WRITE true
  56. #define FLASH_WRITES_PER_WORD 2
  57. #define FLASH_WRITES_PER_PAGE 512
  58. #elif defined(NRF52)
  59. #define FLASH_ERASE_CYCLES 10000
  60. #define FLASH_PAGE_SIZE 4096
  61. #define FLASH_ERASE_PAGE_TIME 90
  62. #define FLASH_SUPPORTS_RANDOM_WRITE true
  63. #define FLASH_WRITES_PER_WORD 32
  64. #define FLASH_WRITES_PER_PAGE 181
  65. #elif defined(NRF52840)
  66. #define FLASH_ERASE_CYCLES 10000
  67. #define FLASH_PAGE_SIZE 4096
  68. #define FLASH_ERASE_PAGE_TIME 90
  69. #define FLASH_SUPPORTS_RANDOM_WRITE true
  70. #define FLASH_WRITES_PER_WORD 2
  71. #define FLASH_WRITES_PER_PAGE 403
  72. #else
  73. #define FLASH_ERASE_CYCLES 10000
  74. #define FLASH_PAGE_SIZE 4096
  75. #define FLASH_ERASE_PAGE_TIME 100
  76. //#define FLASH_SUPPORTS_RANDOM_WRITE true
  77. #define FLASH_WRITES_PER_WORD 1
  78. #warning "Unknown platform. Please check the code."
  79. #endif
  80. /**
  81. * @class FlashClass
  82. * @brief This class provides low-level access to internal Flash memory.
  83. */
  84. class FlashClass
  85. {
  86. public:
  87. //----------------------------------------------------------------------------
  88. /** Constructor */
  89. FlashClass() {};
  90. //----------------------------------------------------------------------------
  91. /** Initialize Flash */
  92. void begin() {};
  93. /** Deinitialize Flash */
  94. void end() {};
  95. //----------------------------------------------------------------------------
  96. /*
  97. * Physical flash geometry
  98. */
  99. //----------------------------------------------------------------------------
  100. /** Page size in bytes
  101. * @return Number of bytes
  102. */
  103. uint32_t page_size() const;
  104. //----------------------------------------------------------------------------
  105. /** Page address width in bits. Page size is 2^x
  106. * @return Number of bits
  107. */
  108. uint8_t page_size_bits() const;
  109. //----------------------------------------------------------------------------
  110. /** Number of managed flash pages
  111. * @return Number of pages
  112. */
  113. uint32_t page_count() const;
  114. //----------------------------------------------------------------------------
  115. /** Number of page erase cycles
  116. * @return Number of page erase cycles
  117. */
  118. uint32_t specified_erase_cycles() const;
  119. //----------------------------------------------------------------------------
  120. /** Get a address of a page
  121. * @param[in] page Page number, starting at 0
  122. * @return address of given page
  123. */
  124. uint32_t *page_address(size_t page);
  125. /** Get top of available flash for application data
  126. * @return Last available address + 1
  127. */
  128. uint32_t *top_app_page_address();
  129. //----------------------------------------------------------------------------
  130. /*
  131. * Accessing flash memory
  132. */
  133. //----------------------------------------------------------------------------
  134. /** Erase a page of given size. Size must be page_size aligned!
  135. * Take care about RADIO, WDT and Interrupt timing!
  136. * @param[in] *address Pointer to page
  137. * @param[in] size number of page aligned bytes to erase
  138. */
  139. void erase(uint32_t *address, size_t size);
  140. //----------------------------------------------------------------------------
  141. /** Erase the complete MCU. This can brick your device!
  142. */
  143. void erase_all();
  144. //----------------------------------------------------------------------------
  145. /** write a aligned 32 bit word to flash.
  146. * @param[in] *address 32 bit aligned pointer to destination word
  147. * @param[in] value Data word to write
  148. */
  149. void write(uint32_t *address, uint32_t value);
  150. //----------------------------------------------------------------------------
  151. /** write a aligned block to flash.
  152. * @param[in] *dst_address 32 bit aligned pointer to destination
  153. * @param[in] *src_address 32 bit aligned pointer to source
  154. * @param[in] word_count Number of words to write
  155. */
  156. void write_block(uint32_t *dst_address, uint32_t *src_address,
  157. uint16_t word_count);
  158. private:
  159. // Wait until flash is ready
  160. void wait_for_ready();
  161. };
  162. extern FlashClass Flash;
  163. /** Load Hardwarespecific files */
  164. #ifdef NRF5
  165. #include "hal/architecture/NRF5/drivers/Flash.cpp"
  166. #else
  167. #error "Unsupported platform."
  168. #endif
  169. /** @} */