PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

157 lines
5.6KB

  1. /*
  2. EEPROM.h - EEPROM library
  3. Original Copyright (c) 2006 David A. Mellis. All right reserved.
  4. New version by Christopher Andrews 2015.
  5. This copy has minor modificatons for use with Teensy, by Paul Stoffregen
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef EEPROM_h
  19. #define EEPROM_h
  20. #include <inttypes.h>
  21. #include <avr/eeprom.h>
  22. #include <avr/io.h>
  23. /***
  24. EERef class.
  25. This object references an EEPROM cell.
  26. Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
  27. This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
  28. ***/
  29. struct EERef{
  30. EERef( const int index )
  31. : index( index ) {}
  32. //Access/read members.
  33. uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); }
  34. operator const uint8_t() const { return **this; }
  35. //Assignment/write members.
  36. EERef &operator=( const EERef &ref ) { return *this = *ref; }
  37. EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; }
  38. EERef &operator +=( uint8_t in ) { return *this = **this + in; }
  39. EERef &operator -=( uint8_t in ) { return *this = **this - in; }
  40. EERef &operator *=( uint8_t in ) { return *this = **this * in; }
  41. EERef &operator /=( uint8_t in ) { return *this = **this / in; }
  42. EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; }
  43. EERef &operator %=( uint8_t in ) { return *this = **this % in; }
  44. EERef &operator &=( uint8_t in ) { return *this = **this & in; }
  45. EERef &operator |=( uint8_t in ) { return *this = **this | in; }
  46. EERef &operator <<=( uint8_t in ) { return *this = **this << in; }
  47. EERef &operator >>=( uint8_t in ) { return *this = **this >> in; }
  48. EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; }
  49. /** Prefix increment/decrement **/
  50. EERef& operator++() { return *this += 1; }
  51. EERef& operator--() { return *this -= 1; }
  52. /** Postfix increment/decrement **/
  53. uint8_t operator++ (int) {
  54. uint8_t ret = **this;
  55. return ++(*this), ret;
  56. }
  57. uint8_t operator-- (int) {
  58. uint8_t ret = **this;
  59. return --(*this), ret;
  60. }
  61. int index; //Index of current EEPROM cell.
  62. };
  63. /***
  64. EEPtr class.
  65. This object is a bidirectional pointer to EEPROM cells represented by EERef objects.
  66. Just like a normal pointer type, this can be dereferenced and repositioned using
  67. increment/decrement operators.
  68. ***/
  69. struct EEPtr{
  70. EEPtr( const int index )
  71. : index( index ) {}
  72. operator const int() const { return index; }
  73. EEPtr &operator=( int in ) { return index = in, *this; }
  74. //Iterator functionality.
  75. bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
  76. EERef operator*() { return index; }
  77. /** Prefix & Postfix increment/decrement **/
  78. EEPtr& operator++() { return ++index, *this; }
  79. EEPtr& operator--() { return --index, *this; }
  80. EEPtr operator++ (int) { return index++; }
  81. EEPtr operator-- (int) { return index--; }
  82. int index; //Index of current EEPROM cell.
  83. };
  84. /***
  85. EEPROMClass class.
  86. This object represents the entire EEPROM space.
  87. It wraps the functionality of EEPtr and EERef into a basic interface.
  88. This class is also 100% backwards compatible with earlier Arduino core releases.
  89. ***/
  90. struct EEPROMClass{
  91. #if defined(__arm__) && defined(TEENSYDUINO)
  92. EEPROMClass() { eeprom_initialize(); }
  93. #endif
  94. //Basic user access methods.
  95. EERef operator[]( const int idx ) { return idx; }
  96. uint8_t read( int idx ) { return EERef( idx ); }
  97. void write( int idx, uint8_t val ) { (EERef( idx )) = val; }
  98. void update( int idx, uint8_t val ) { EERef( idx ).update( val ); }
  99. //STL and C++11 iteration capability.
  100. EEPtr begin() { return 0x00; }
  101. EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
  102. uint16_t length() { return E2END + 1; }
  103. //Functionality to 'get' and 'put' objects to and from EEPROM.
  104. template< typename T > T &get( int idx, T &t ){
  105. EEPtr e = idx;
  106. uint8_t *ptr = (uint8_t*) &t;
  107. for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
  108. return t;
  109. }
  110. template< typename T > const T &put( int idx, const T &t ){
  111. const uint8_t *ptr = (const uint8_t*) &t;
  112. #ifdef __arm__
  113. eeprom_write_block(ptr, (void *)idx, sizeof(T));
  114. #else
  115. EEPtr e = idx;
  116. for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ );
  117. #endif
  118. return t;
  119. }
  120. };
  121. static EEPROMClass EEPROM __attribute__ ((unused));
  122. #endif