uEEPROMLib
https://github.com/Naguissa/uEEPROMLib
uEEPROMLib.h
Go to the documentation of this file.
1 
16 #ifndef UEEPROMLIB
20  #define UEEPROMLIB
21  #include "Arduino.h"
22  #ifndef UEEPROMLIB_WIRE
23  #if defined(ARDUINO_attiny) || defined(ARDUINO_AVR_ATTINYX4) || defined(ARDUINO_AVR_ATTINYX5) || defined(ARDUINO_AVR_ATTINYX7) || defined(ARDUINO_AVR_ATTINYX8) || defined(ARDUINO_AVR_ATTINYX61) || defined(ARDUINO_AVR_ATTINY43) || defined(ARDUINO_AVR_ATTINY828) || defined(ARDUINO_AVR_ATTINY1634) || defined(ARDUINO_AVR_ATTINYX313)
24  #include <TinyWireM.h> // I2C Master lib for ATTinys which use USI
25  #define UEEPROMLIB_WIRE TinyWireM
26  #else
27  #include <Wire.h>
28  #define UEEPROMLIB_WIRE Wire
29  #endif
30  #endif
31 
37  #define UEEPROMLIB_ADDRESS 0x57
38 
39  // ESP yield function (ESP32 has no need for that on dual core, but it has on single core version)
40  #if ARDUINO_ARCH_ESP8266
46  #define uEEPROMLIB_YIELD yield();
47  #else
48  #if ARDUINO_ARCH_ESP32
54  #define uEEPROMLIB_YIELD yield();
55  #else
56  #define uEEPROMLIB_YIELD
57  #endif
58  #endif
59 
60 
64  #define uEEPROMLIB_WIRE_DELAY 6
65 
69  #define uEEPROMLIB_WIRE_SHORT_DELAY 1
70 
71  // BUFFER_LENGTH should come from Wire library
72  #ifdef BUFFER_LENGTH
76  #define UEEPROMLIB_WIRE_MAX_RBUFFER (BUFFER_LENGTH - 2)
77  #else
81  #define UEEPROMLIB_WIRE_MAX_RBUFFER 32
82  #endif
83  #define UEEPROMLIB_WIRE_MAX_WBUFFER (UEEPROMLIB_WIRE_MAX_RBUFFER - 2)
84 
85 
86  class uEEPROMLib {
87  public:
88  // Constructors
89  uEEPROMLib();
90  uEEPROMLib(const int);
91  uEEPROMLib(bool);
92  uEEPROMLib(bool, const int);
93 
94  void set_address(const uint8_t);
95  // EEPROM read functions
96  bool eeprom_read(const unsigned int, byte *, unsigned int);
97  template <typename TR> void eeprom_read(const unsigned int, TR *);
98  byte eeprom_read(const unsigned int);
99 
100  // EEPROM write functions
101  bool eeprom_write(const unsigned int, void *, const unsigned int);
102  bool eeprom_write(const unsigned int, char);
103  bool eeprom_write(const unsigned int, unsigned char);
104  template <typename TW> bool eeprom_write(const unsigned int, const TW);
105 
109  uint8_t page_size = 32; // common size, as AT24C32 (datasheet on extras)
110 
111  private:
112  bool _eeprom_read_sub(const unsigned int, byte *, const uint8_t);
113  bool _eeprom_write_sub(const unsigned int, byte *, const uint8_t);
114 
115  // Addresses
116  int _ee_address = UEEPROMLIB_ADDRESS;
117 
118  // EEPROM read and write private functions - works with bytes
119  byte _eeprom_read(const unsigned int);
120  bool _eeprom_write(const unsigned int, const byte);
121  // Fix for 1st write error
122  bool init = false;
123  #define uEEPROMLIB_STM32_INIT_FIX() { if (!init) { init = true; _eeprom_read(0); delay(10); } }
124  };
125 
126 
127  // Templates must be here because Arduino compiler incompatibility to declare them on .cpp file
128 
144  template <typename TW> bool uEEPROMLib::eeprom_write(const unsigned int address, const TW data) {
145  return eeprom_write(address, (void *) &data, sizeof(TW));
146  }
147 
148 
155  template <typename TR> void uEEPROMLib::eeprom_read(const unsigned int address, TR *data) {
156  eeprom_read(address, (byte *) data, sizeof(TR));
157  }
158 #endif
159 
160 
I2C EEPROM library. Split from uRTCLib.
Definition: uEEPROMLib.h:86
uEEPROMLib()
Constructor.
Definition: uEEPROMLib.cpp:28
bool eeprom_write(const unsigned int, void *, const unsigned int)
Write sequence of n bytes.
Definition: uEEPROMLib.cpp:239
uint8_t page_size
: EEPROM page size control
Definition: uEEPROMLib.h:109
bool eeprom_read(const unsigned int, byte *, unsigned int)
Read sequence of n bytes.
Definition: uEEPROMLib.cpp:112
void set_address(const uint8_t)
Sets EEPROM i2c addres.
Definition: uEEPROMLib.cpp:71
#define UEEPROMLIB_ADDRESS
Default EEPROM address.
Definition: uEEPROMLib.h:37