How to: EEPROM

EEPROM is a special kind of memory. It is small (6 KB on Core module chip) memory with limited number of write/erase cycles. It is non-volatile memory - which means that it does not require power to retain stored information. Which means that bytes written/stored inside the EEPROM will stay there until erased/rewritten.

Don’t be afraid of limited W/E cycles. In standard conditions the chip guarantees 100 000 cycles. Remember that those cycles are Write/Erase


Reading from the EEPROM do not count, so it is completely safe to read from it as much as you wish.


HARDWARIO TOWER Core module has 6 KB EEPROM included. In case you need to find this value out inside your code, there is a function for this inside the SDK: size_t twr_eeprom_get_size(void)


Writing to EEPROM is very easy. It takes only one call to this function: bool twr_eeprom_write(uint32_t address, const void *buffer, size_t length)

Parameters taken:

  • address - start address (starts at 0, ends at 6143)

  • buffer - pointer to source buffer, from which data will be red (can be any type - int, char, float, …)

  • length - number of bytes to by written

Returned value indicates whether the write process was successful or not.

It is completely up to you from what address you start to write. You can start from 0, 42, 666,… all the way up to circa 6000.


Please note that some of our modules (currently twr_radio_* module only) use few last dozens of bytes in EEPROM. If you use those modules, remember to use the memory addresses from 0 up to 6000. This makes sure that no data will be overwritten.

Always make sure that you have correctly chosen the length parameter. If you want to write some numeric value, you can use the sizeof() function to find out the correct number. Let’s say that you have a float variable and you want to write this number to address 0:

1float var = 3.14159;
2twr_eeprom_write(0, &var, sizeof(var));


Reading from the EEPROM is similar to writing. There is appropriate function for this task: bool c_eeprom_read(uint32_t address, void *buffer, size_t length)

Same parameters and return value purpose. Only exception is that the buffer is now pointer to the destination buffer where data from EEPROM will be written to.

Let’s say that now you want to read previously saved value and store it inside variable called buf. You also know that float value has size of four bytes:

1float buf;
2twr_eeprom_read(0, &buf, 4);

R/W Example

In this example we will write float value and string to EEPROM immediately after Core boot. On every press of a button the data will be retrieved from EEPROM and sent to computer. To test that the memory is really persistent you can try to comment both twr_eeprom_write lines out (after running the original example once, of course). It should still work.

The output in serial console will look like this:

EEPROM size: 6144
hello world!


 1#include <application.h>
 3twr_button_t button;
 5void button_event_handler(twr_button_t *self, twr_button_event_t event, void *event_param)
 7    (void) self;
 8    (void) event_param;
10    if (event == TWR_BUTTON_EVENT_PRESS)
11    {
12        size_t eeprom = twr_eeprom_get_size();
13        char readEeprom[13];
14        float readFloat;
16        twr_eeprom_read(0, &readFloat, 4);
17        twr_eeprom_read(4, readEeprom, 12);
18        readEeprom[12] = '\0';
20        twr_log_debug("EEPROM size: %d\r\nData:\r\n%f\r\n%s", eeprom, readFloat, readEeprom);
21    }
24void application_init(void)
28    float toWriteFloat = 3.14159;
29    char toWrite[] = "hello world!";
30    twr_eeprom_write(0, &toWriteFloat, sizeof(toWriteFloat));
31    twr_eeprom_write(sizeof(toWriteFloat), toWrite, sizeof(toWrite));
33    // Initialize button
34    twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN, false);
35    twr_button_set_event_handler(&button, button_event_handler, NULL);