Esp8266: Read and Write from/to EEPROM (Flash Memory)

Содержание

Esp8266 EEPROM

Like on a PC, data stored in RAM is lost during a reboot or when power is lost. Similarly, data that is stored on flash memory (e.g. a USB-Stick) remains intact.

Thus, we will store data that cannot be lost during power loss in the esp8266’s flash memory. This is similar to storing data in EEPROM on an Arduino. So, when we are talking about EEPROM, we are actually referring to EEPROM emulation using the esp’s flash memory.

EEPROM Emulation

As the esp8266 does not come with real EEPROM we emulate it using the built-in flash memory.

Depending on the esp8266 model the amount of flash memory can vary from 512KiB to 4MiB.

While for example, the esp-01 comes with 512KiB or 1MiB flash memory, the esp-12f has 4MiB built-in. You can find the exact amount built-into the model you are using in the datasheet.

Note: The flash memory is also used to store the program code itself.

esp8266 EEPROM-Flash mapFigure 1: Simplified esp8266 flash map using EEPROM

EEPROM.begin

Unlike using an Arduino we have to start the EEPROM emulation (before reading and writing) with

This allocates a SIZE-bytes large block of RAM and copies the data from flash. SIZE can be anywhere between a minimum of 4 and a maximum of 4096.

From now on we work with the copy in RAM like we would with a normal array.

Calculating Required EEPROM Size

We now have to decide how much flash memory we want to use for EEPROM emulation. The maximum amount of flash we can use is one sector (4096B).

If we want to store standard data types, we can use the cheatsheet below:

1 byte 2 bytes 4 bytes 8 bytes
bool short int double
char uint16_t long long long
byte float uint64_t
uint8_t uint32_t

However, the size of these data types can vary.

So, in general, the \(sizeof()\) function should be used to determine the amount of memory (in bytes) required to store a variable of a given type.

Example

If we want to store an int we need the EEPROM size to be at least \(sizeof(int)\) bytes.

If we want to store an int and a 20 character long char array (string), we need \(sizeof(int)+20\cdot sizeof(char)\) bytes.

Note: As we will later see it is also important to remember which variable is stored at which address in EEPROM. For the second example, we could store the int at \(addr_1=0\) and the string at \(addr_2=sizeof(int)\).

To simplify reading and writing to EEPROM, we can use structs as shown in the next section.

Read and Write to/from EEPROM

Now that we know the required EEPROM size, we can use the Arduino EEPROM library to read and write bytes to and from EEPROM (flash memory).

As an example, we will now store the wifi SSID and password in EEPROM:

To start we will include the EEPROM library and use EEPROM.begin to allocate the required amount of memory.

Note: You can download the complete example below.

How to Read from EEPROM

EEPROM.get

The simplest way to read data from EEPROM is using the EEPROM.get() function.

EEPROM.read

If necessary, we can also read individual bytes from EEPROM using EEPROM.read():

How to Write to EEPROM

As mentioned above, we are working with an array in RAM, not directly with the data stored in flash/EEPROM. We, therefore, have to use the EEPROM.commit() function to save changes in RAM to flash.

This will get clearer as you take a look at the two examples below:

EEPROM.put

The simplest way to store data in EEPROM is using the EEPROM.put() function:

EEPROM.write

We can write individual bytes using EEPROM.write():

EEPROM.commit

Write changes to the EEPROM data in RAM to flash memory.

If there are no changes in RAM, this function does nothing. If a single byte is changed in RAM, the complete EEPROM array in RAM is written to flash memory.


Источник: blog.hirnschall.net