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.

VirtualPage.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. VirtualPage.h - Flash page management
  3. Original Copyright (c) 2017 Frank Holtz. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. /**
  17. * @file VirtualPage.h
  18. * @brief Virtual page management on top of Flash
  19. * The managed pages are organized into VirtualPage.size() sized pages.
  20. * Some Bytes of a Flash page are reserved for a magic number and page management.
  21. *
  22. * @ingroup NVM
  23. * @details Nonvolatile Memory Class
  24. * @{
  25. */
  26. #pragma once
  27. #include "Flash.h"
  28. #include <Arduino.h>
  29. /**
  30. * @class VirtualPageClass
  31. * @brief Virtual page management on top of Flash
  32. */
  33. class VirtualPageClass
  34. {
  35. public:
  36. //----------------------------------------------------------------------------
  37. /** Constructor */
  38. VirtualPageClass() {};
  39. //----------------------------------------------------------------------------
  40. /** Initialize Virtual Pages */
  41. void begin() {};
  42. //----------------------------------------------------------------------------
  43. /** Deinitilize Virtual Pages */
  44. void end() {};
  45. //----------------------------------------------------------------------------
  46. /** Reports usable page size in bytes
  47. * @return Number of bytes
  48. */
  49. uint16_t size() const;
  50. //----------------------------------------------------------------------------
  51. /** Reports usable page size in 32 Bit words
  52. * @return Number of words
  53. */
  54. uint16_t length() const;
  55. //----------------------------------------------------------------------------
  56. /** Reports the maximum number of allocatable pages
  57. * @return Number of pages
  58. */
  59. uint16_t page_count() const;
  60. //----------------------------------------------------------------------------
  61. /** Calculates the rate of wear in percent*100.
  62. * Values greater than 10000 indicates exceeding the chip specification.
  63. * This value is only valid for controllers, never erased completely.
  64. * @return calculated level
  65. */
  66. uint32_t wear_level();
  67. //----------------------------------------------------------------------------
  68. /** Search for a page by given, unique magic number.
  69. * Returns a pointer to (uint32_t *)~0 if there is no page. Don't write
  70. * to this address.
  71. * @param[in] magic A magic number to identify the page.
  72. * @return Pointer to a page
  73. */
  74. uint32_t *get(uint32_t magic);
  75. //----------------------------------------------------------------------------
  76. /** Returns an address to a blank page or (uint32_t *)~0 if no space
  77. * available. Take care about RADIO, WDT and Interrupt timing! Calculate with
  78. * 0-100ms until a page is available.
  79. * @param[in] magic A magic number to identify the page.
  80. * @return Pointer to a page
  81. */
  82. uint32_t *allocate(uint32_t magic);
  83. //----------------------------------------------------------------------------
  84. /** Returns an address to a blank page or (uint32_t *)~0 if no space
  85. * available. Take care about RADIO, WDT and Interrupt timing! Calculate with
  86. * 0-100ms until a page is available.
  87. * This function allows using a page multiple times without erasing on some platforms.
  88. * @param[in] magic A magic number to identify the page.
  89. * @param[in] max_writes The number of planned write operations in page lifecycle.
  90. * @return Pointer to a page
  91. */
  92. uint32_t *allocate(uint32_t magic, uint32_t max_writes);
  93. //----------------------------------------------------------------------------
  94. /** Start releasing a page. You have to allocate a new one and then release the
  95. * old page.
  96. * @param[in] *address A pointer to the page to release
  97. */
  98. void release_prepare(uint32_t *address);
  99. //----------------------------------------------------------------------------
  100. /** End releasing a page.
  101. * @param[in] *address A pointer to the page to release
  102. */
  103. void release(uint32_t *address);
  104. //----------------------------------------------------------------------------
  105. /** Returns true if page is in release_prepare state
  106. * @param[in] *address A pointer to a page
  107. * @return Release state
  108. */
  109. bool release_started(uint32_t *address);
  110. //----------------------------------------------------------------------------
  111. /** Mark a page as a defect page.
  112. */
  113. void fail(uint32_t *address);
  114. //----------------------------------------------------------------------------
  115. /** Prepare released pages for faster reallocation. Plan with 0-100ms.
  116. */
  117. void clean_up();
  118. //----------------------------------------------------------------------------
  119. /** Release all pages
  120. */
  121. void format();
  122. private:
  123. // convert page number 1..max_pages into an address
  124. uint32_t *get_page_address(uint16_t page);
  125. // build a page
  126. void build_page(uint32_t *address, uint32_t magic);
  127. // return number of erase cycles
  128. uint32_t get_page_erase_cycles(uint32_t *address);
  129. };
  130. extern VirtualPageClass VirtualPage;
  131. /** @} */