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.

NVRAM.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * NVRAM.h - Byte-wise storage for Virtual Pages.
  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 NVRAM.h
  21. * @brief Byte-wise storage for Virtual Pages.
  22. *
  23. * @defgroup NVM Nonvolatile Memory
  24. * @ingroup internals
  25. * @details Nonvolatile Memory Class
  26. * @{
  27. */
  28. #pragma once
  29. #include "Flash.h"
  30. #include "VirtualPage.h"
  31. #include <Arduino.h>
  32. /**
  33. * @class NVRAMClass
  34. * @brief Nonvolatile Memory
  35. */
  36. class NVRAMClass
  37. {
  38. public:
  39. //----------------------------------------------------------------------------
  40. /** Constructor. */
  41. NVRAMClass() {};
  42. //----------------------------------------------------------------------------
  43. /** Initialize Class */
  44. void begin() {};
  45. //----------------------------------------------------------------------------
  46. /** Deinitialize Class */
  47. void end() {};
  48. //----------------------------------------------------------------------------
  49. /** NVM available space in bytes
  50. * @return Number of bytes
  51. */
  52. uint16_t length() const;
  53. //----------------------------------------------------------------------------
  54. /** Read a block of bytes
  55. * @param[in] *dst Write bytes to given pointer
  56. * @param[in] idx First NVM address to read
  57. * @param[in] n Number of bytes to read
  58. */
  59. void read_block(uint8_t *dst, uint16_t idx, uint16_t n);
  60. //----------------------------------------------------------------------------
  61. /** Read a byte
  62. * @param[in] idx NVM Address to read
  63. * @return Byte from given address
  64. */
  65. uint8_t read(const uint16_t idx);
  66. //----------------------------------------------------------------------------
  67. /** Write a block
  68. * @param[in] *src Read bytes from given pointer
  69. * @param[in] idx First NVM address to write
  70. * @param[in] n Number of bytes to write
  71. * @return success
  72. */
  73. bool write_block(uint8_t *src, uint16_t idx, uint16_t n);
  74. //----------------------------------------------------------------------------
  75. /** Write a byte
  76. * @param[in] idx NVM Address to write
  77. * @param[in] value Byte to write
  78. * @return success
  79. */
  80. bool write(const uint16_t idx, uint8_t value);
  81. //----------------------------------------------------------------------------
  82. /** Preserve the number of bytes in NVM log for time critical writes. Runtime
  83. * up to 5000ms!
  84. * @param[in] number Bytes to preserve, can 0 to find out free log space
  85. * @return Bytes available for writing
  86. */
  87. int write_prepare(uint16_t number);
  88. //----------------------------------------------------------------------------
  89. /** lear log if full and prepare released pages for faster reallocation.
  90. * Runtime up to 5000ms!
  91. * @param[in] write_preserve Byte to preserve
  92. */
  93. void clean_up(uint16_t write_preserve);
  94. private:
  95. // Return a virtual page
  96. uint32_t *get_page();
  97. // Get actual log position
  98. uint16_t get_log_position(uint32_t *vpage);
  99. // Read a byte from page
  100. uint8_t get_byte_from_page(uint32_t *vpage, uint16_t log_start,
  101. uint16_t log_end, uint16_t idx);
  102. // switch a page
  103. uint32_t *switch_page(uint32_t *old_vpage, uint16_t *log_start,
  104. uint16_t *log_end);
  105. };
  106. /** Variable to access the NVRAMClass */
  107. extern NVRAMClass NVRAM;
  108. /** @} */