> For the complete documentation index, see [llms.txt](https://cavedu.gitbook.io/linkit-7697/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cavedu.gitbook.io/linkit-7697/linkit-7697-development-guide-for-arduino-ide/developer-guide/eeprom.md).

# EEPROM

EEPROM is the memory whose values are kept when the board is turned off (like a tiny hard drive). It provides a non-volatile data storage for developers. This [EEPROM library](https://www.arduino.cc/en/Reference/EEPROM) enables you to read and write data to/from a emulated EEPROM (implemented by [NVDM](http://labs.mediatek.com/api/mt7697/group___n_v_d_m.html): **N**on-**V**olatile **D**ata **M**anagement, which is the flash storage access mechanism provided in the LinkIt SDK) on LinkIt 7697 and the total accessible size of the EEPROM is **1KB**.

{% hint style="danger" %}
The total accessible storage size for NVDM on LinkIt 7697 is 64KB. And this storage partition is shared between the LFlash class and the EEPROM class, which are both implemented by NVDM. Therefore, if you consume all the 1KB storage provided by the EEPROM class, you'll have 63KB left for LFlash. If you don't need any storage for EEPROM, then LFlash can use all the 64KB storage allocated to NVDM.

In short, size(LFlash + EEPROM) := 64KB and max\_size(EEPROM) := 1KB.
{% endhint %}

### Usage <a href="#eeprom-usage" id="eeprom-usage"></a>

Before using EEPROM APIs, you need to include the header file first:

```
#include <EEPROM.h>
```

There are 6 APIs you can use to access the EEPROM:

* length()
* read()
* write()
* update()
* get()
* put()

#### Basic operations <a href="#eeprom-basicoperations" id="eeprom-basicoperations"></a>

By calling *EEPROM.length()*, developers can retrieve the total size of the EEPROM. On LinkIt 7697, it'll return 1024.

For a single-byte access, the *EEPROM.read()*, *EEPROM.write()* and *EEPROM.update()* APIs can be used. Function prototypes of above APIs are:

```
uint8_t read(int idx);
void write(int idx, uint8_t val);
void update(int idx, uint8_t val);
```

The definitions of API arguments are:

* **idx**: the address used to read or write data byte. The valid value is within the range 0 \~ EEPROM.length() - 1.&#x20;
* **val**: the data byte to be written into the EEPROM.

Due to the limited write cycles of a flash storage, it's not recommended to write to a flash storage frequently. By using the *.update()* API to do the write operation, it will first examine if the data to be written is identical to the data on the flash. If they are the same, the *.update()* call would take no effect. If they are not the same, the API call will trigger a flash write operation to finish the user demand.

On the other hand, if the *.write()* API is used instead of the *.update()*, the flash write operation will always be triggered, no matter what the data to be updated is.

#### Access through \[] operators <a href="#eeprom-accessthrough-operators" id="eeprom-accessthrough-operators"></a>

Not only by using API calls, the EEPROM can also be accessed by \[] operators. For examples:

```
// read the byte from address 88 and store it to the 'data' variable
uint8_t data = EEPROM[88];
// write a byte (the value is 97) to address 76
EEPROM[76] = 97;
// increase the value in address 2 by 1
EEPROM[2]++;
```

#### Multi-byte access <a href="#eeprom-multi-byteaccess" id="eeprom-multi-byteaccess"></a>

EEPROM can be accessed not only in a single-byte manner, the I/O can also be in a multi-byte way. The *.get()* and *.put()* APIs provide such functionalities. The prototypes of them are:

```
template<typename T> T &get(int idx, T &t);
template<typename T> const T &put(int idx, const T &t);
```

The definitions of API arguments:

* **idx**: the starting address to read or write data bytes.
* **t**: the data structure storing the data to be written to or to be read from the EEPROM. &#x20;

Here is an example for *.get()* and *.put()*:

```
// define the data type
typedef struct
{
  int id;
  float value;
  char ch;
} data_t;
 
data_t input, output;
int address = 76;
 
// init the values for input
input.id = 10;
input.value = 123.456f;
input.ch = 'c';
 
// store the input data to the EEPROM
put(address, &input);
// after the input is stored, we can read it back from the same address
get(address, &output);
```

In the above example, a multi-byte data structure (*data\_t*) is stored to the EEPROM by using *.put()*. And it can be read back by using *.get()*. The read back data should be identical to the input data.

For more examples, you can refer to **Files / Examples / EEPROM** in the Arduino IDE to see different applications.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cavedu.gitbook.io/linkit-7697/linkit-7697-development-guide-for-arduino-ide/developer-guide/eeprom.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
