侧边栏壁纸
  • 累计撰写 131 篇文章
  • 累计创建 16 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录
ESP32   

ESP32-EEPROM存储

EEPROM前言

在EPS32中已经将EEPROM弃用。对于ESP32上的新应用程序,建议使用NVS为首选项。提供EEPROM是为了向后兼容现有的Arduino应用程序。EEPROM是使用NVS中的单个blob实现的,因此它是容器(Flash)中的容器(NVS)(弟中弟)。因此,它不会是一种高性能存储方法。首选项将直接使用nvs,并将每个条目存储为其中的单个对象。所以现在的EEPROM也仅是在函数功能上向后兼容,实际储存方式已经完全变了,这需要我们在实际应用中注意。NVS小知识链接

主要API介绍

bool begin(size_t size);   // 开启一块分区访问存储
uint8_t read(int address);  // 读取指定地址数据
void write(int address, uint8_t val); // 在指定地址保存数据
uint16_t length();   // 获取申请的分区大小
bool commit();  // 将数据从缓存区存入flash中
void end();  // 结束访问

在实际应用中主要思路是,先申明begin(想要保存的数据大小),再利用write(地址偏移,单个字符数据0-255),当所有数据通过write写完后,一定要记得中commit()提交,使数据从暂存区保存到flash中,实现掉电保护。read的使用直接利用地址偏移可以直接读出数据。

例子1(随机数的保存)

#include <Arduino.h>
#include "EEPROM.h"

// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;
#define EEPROM_SIZE 64
void setup()
{
  Serial.begin(115200);
  Serial.println("start...");
  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to initialise EEPROM"); delay(1000000);
  }
  Serial.println(" bytes read from Flash . Values are:");
  for (int i = 0; i < EEPROM_SIZE; i++)
  {
    Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
  }
  Serial.println();
  Serial.println("writing random n. in memory");
}

void loop()
{
  // need to divide by 4 because analog inputs range from
  // 0 to 1023 and each byte of the EEPROM can only hold a
  // value from 0 to 255.
  // int val = analogRead(10) / 4;
  int val = byte(random(10020));
  // write the value to the appropriate byte of the EEPROM.
  // these values will remain there when the board is
  // turned off.
  EEPROM.write(addr, val);
  Serial.print(val); Serial.print(" ");
  // advance to the next address.  there are 512 bytes in
  // the EEPROM, so go back to 0 when we hit 512.
  // save all changes to the flash.
  addr = addr + 1;
  if (addr == EEPROM_SIZE)
  {
    Serial.println();
    addr = 0;
    EEPROM.commit();
    Serial.print(EEPROM_SIZE);
    Serial.println(" bytes written on Flash . Values are:");
    for (int i = 0; i < EEPROM_SIZE; i++)
    {
      Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
    }
    Serial.println(); Serial.println("----------------------------------");
  }

  delay(100);
}

特殊API直接调用

前面的写入与读取只能是单字符的读取与写入,官方为了方便调用,还提供了对于各数据类型的支持:

    uint8_t readByte(int address);
    int8_t readChar(int address);
    uint8_t readUChar(int address);
    int16_t readShort(int address);
    uint16_t readUShort(int address);
    int32_t readInt(int address);
    uint32_t readUInt(int address);
    int32_t readLong(int address);
    uint32_t readULong(int address);
    int64_t readLong64(int address);
    uint64_t readULong64(int address);
    float_t readFloat(int address);
    double_t readDouble(int address);
    bool readBool(int address);
    size_t readString(int address, char* value, size_t maxLen);
    String readString(int address);
    size_t readBytes(int address, void * value, size_t maxLen);
    template <class T> T readAll (int address, T &);

    size_t writeByte(int address, uint8_t value);
    size_t writeChar(int address, int8_t value);
    size_t writeUChar(int address, uint8_t value);
    size_t writeShort(int address, int16_t value);
    size_t writeUShort(int address, uint16_t value);
    size_t writeInt(int address, int32_t value);
    size_t writeUInt(int address, uint32_t value);
    size_t writeLong(int address, int32_t value);
    size_t writeULong(int address, uint32_t value);
    size_t writeLong64(int address, int64_t value);
    size_t writeULong64(int address, uint64_t value);
    size_t writeFloat(int address, float_t value);
    size_t writeDouble(int address, double_t value);
    size_t writeBool(int address, bool value);
    size_t writeString(int address, const char* value);
    size_t writeString(int address, String value);
    size_t writeBytes(int address, const void* value, size_t len);
    template <class T> T writeAll (int address, const T &);

例子2(各数据类型)

#include <Arduino.h>
#include "EEPROM.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\nTesting EEPROM Library\n");
  if (!EEPROM.begin(1000)) {
    Serial.println("Failed to initialise EEPROM");
    Serial.println("Restarting...");
    delay(1000);
    ESP.restart();
  }

  int address = 0;

  EEPROM.writeByte(address, -128);                  // -2^7
  address += sizeof(byte);

  EEPROM.writeChar(address, 'A');                   // Same as writyByte and readByte
  address += sizeof(char);

  EEPROM.writeUChar(address, 255);                  // 2^8 - 1
  address += sizeof(unsigned char);

  EEPROM.writeShort(address, -32768);               // -2^15
  address += sizeof(short);

  EEPROM.writeUShort(address, 65535);               // 2^16 - 1
  address += sizeof(unsigned short);

  EEPROM.writeInt(address, -2147483648);            // -2^31
  address += sizeof(int);

  EEPROM.writeUInt(address, 4294967295);            // 2^32 - 1
  address += sizeof(unsigned int);

  EEPROM.writeLong(address, -2147483648);           // Same as writeInt and readInt
  address += sizeof(long);

  EEPROM.writeULong(address, 4294967295);           // Same as writeUInt and readUInt
  address += sizeof(unsigned long);

  int64_t value = -1223372036854775808LL;             // -2^63
  EEPROM.writeLong64(address, value);
  address += sizeof(int64_t);

  uint64_t  Value = 18446744073709551615ULL;           // 2^64 - 1
  EEPROM.writeULong64(address, Value);
  address += sizeof(uint64_t);

  EEPROM.writeFloat(address, 1234.1234);
  address += sizeof(float);

  EEPROM.writeDouble(address, 123456789.123456789);
  address += sizeof(double);

  EEPROM.writeBool(address, true);
  address += sizeof(bool);

  String sentence = "I love ESP32.";
  EEPROM.writeString(address, sentence);
  address += sentence.length() + 1;

  char gratitude[21] = "Thank You Espressif!";
  EEPROM.writeString(address, gratitude);
  address += 21;

  // See also the general purpose writeBytes() and readBytes() for BLOB in EEPROM library
  EEPROM.commit();
  address = 0;
  
  Serial.println(EEPROM.readByte(address));
  address += sizeof(byte);

  Serial.println((char)EEPROM.readChar(address));
  address += sizeof(char);

  Serial.println(EEPROM.readUChar(address));
  address += sizeof(unsigned char);

  Serial.println(EEPROM.readShort(address));
  address += sizeof(short);

  Serial.println(EEPROM.readUShort(address));
  address += sizeof(unsigned short);

  Serial.println(EEPROM.readInt(address));
  address += sizeof(int);

  Serial.println(EEPROM.readUInt(address));
  address += sizeof(unsigned int);

  Serial.println(EEPROM.readLong(address));
  address += sizeof(long);

  Serial.println(EEPROM.readULong(address));
  address += sizeof(unsigned long);

  value = 0;
  value = EEPROM.readLong64(value);
  Serial.printf("0x%08X", (uint32_t)(value >> 32)); // Print High 4 bytes in HEX
  Serial.printf("%08X\n", (uint32_t)value);         // Print Low 4 bytes in HEX
  address += sizeof(int64_t);

  Value = 0;                                        // Clear Value
  Value = EEPROM.readULong64(Value);
  Serial.printf("0x%08X", (uint32_t)(Value >> 32)); // Print High 4 bytes in HEX
  Serial.printf("%08X\n", (uint32_t)Value);         // Print Low 4 bytes in HEX
  address += sizeof(uint64_t);

  Serial.println(EEPROM.readFloat(address), 4);
  address += sizeof(float);

  Serial.println(EEPROM.readDouble(address), 8);
  address += sizeof(double);

  Serial.println(EEPROM.readBool(address));
  address += sizeof(bool);

  Serial.println(EEPROM.readString(address));
  address += sentence.length() + 1;

  Serial.println(EEPROM.readString(address));
  address += 21;
}

void loop() {
  // put your main code here, to run repeatedly:
}

初步展现底层:

前面提到过,在目前的ESP32中并没有EEPROM,而是利用NVS模拟出来的,仅是了程序的向后兼容性,而前面的程序中的EEPROM储存空间也仅仅是NVS的单个blob实现的,也就是说我们可以创造多个EEPROM来存储数据。

在EEPROMClass类中,有三个:

        EEPROMClass(uint32_t sector);
        EEPROMClass(const char* name, uint32_t user_defined_size);
        EEPROMClass(void);

程序在默认状态下使用的是最后一个:extern EEPROMClass EEPROM;而从定义中,我们可知这个类是可以指定名称与大小的。

且提供了函数get与put用于保存数据与读取:

    template<typename T>
    T &get(int address, T &t) {
      if (address < 0 || address + sizeof(T) > _size)
        return t;

      memcpy((uint8_t*) &t, _data + address, sizeof(T));
      return t;
    }

    template<typename T>
    const T &put(int address, const T &t) {
      if (address < 0 || address + sizeof(T) > _size)
        return t;

      memcpy(_data + address, (const uint8_t*) &t, sizeof(T));
      _dirty = true;
      return t;
    }

例子(对EEPROMClass的调用):

#include <Arduino.h>
#include "EEPROM.h"

// Instantiate eeprom objects with parameter/argument names and sizes
EEPROMClass  NAMES("eeprom0", 0x500);
EEPROMClass  HEIGHT("eeprom1", 0x200);
EEPROMClass  AGE("eeprom2", 0x100);

void setup() {
  Serial.begin(9600);
  Serial.println("Testing EEPROMClass\n");
  if (!NAMES.begin(NAMES.length())) {
    Serial.println("Failed to initialise NAMES");
    Serial.println("Restarting...");
    delay(1000);
    ESP.restart();
  }
  if (!HEIGHT.begin(HEIGHT.length())) {
    Serial.println("Failed to initialise HEIGHT");
    Serial.println("Restarting...");
    delay(1000);
    ESP.restart();
  }
  if (!AGE.begin(AGE.length())) {
    Serial.println("Failed to initialise AGE");
    Serial.println("Restarting...");
    delay(1000);
    ESP.restart();
  }

  const char* name = "Teo Swee Ann";
  char rname[32];
  double height = 5.8;
  uint32_t age = 47;

  // Write: Variables ---> EEPROM stores
  NAMES.put(0, name);
  HEIGHT.put(0, height);
  AGE.put(0, age);
  Serial.print("name: ");   Serial.println(name);
  Serial.print("height: "); Serial.println(height);
  Serial.print("age: ");    Serial.println(age);
  Serial.println("------------------------------------\n");

  // Clear variables
  name = '\0';
  height = 0;
  age = 0;
  Serial.print("name: ");   Serial.println(name);
  Serial.print("height: "); Serial.println(height);
  Serial.print("age: ");    Serial.println(age);
  Serial.println("------------------------------------\n");

  // Read: Variables <--- EEPROM stores
  NAMES.get(0, rname);
  HEIGHT.get(0, height);
  AGE.get(0, age);
  Serial.print("name: ");   Serial.println(rname);
  Serial.print("height: "); Serial.println(height);
  Serial.print("age: ");    Serial.println(age);
  
  Serial.println("Done!");
}

void loop() {
  delay(0xFFFFFFFF);
}
0

评论区