Skip to content

Commit 1d030d6

Browse files
author
Purva Yeshi
committed
Add EEPROM library
1 parent 211d5dc commit 1d030d6

File tree

8 files changed

+247
-0
lines changed

8 files changed

+247
-0
lines changed

libraries/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
add_subdirectory(Wire)
44
add_subdirectory(SPI)
5+
add_subdirectory(EEPROM)
6+

libraries/EEPROM/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
zephyr_include_directories(.)
3+
4+
if(NOT DEFINED ARDUINO_BUILD_PATH)
5+
zephyr_sources_ifdef(CONFIG_EEPROM EEPROM.cpp)
6+
endif()

libraries/EEPROM/EEPROM.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2024 Purva Yeshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/reboot.h>
9+
#include <zephyr/device.h>
10+
#include <string.h>
11+
#include <zephyr/drivers/flash.h>
12+
#include <zephyr/storage/flash_map.h>
13+
#include <zephyr/fs/nvs.h>
14+
#include <zephyr/logging/log.h>
15+
16+
#include "EEPROM.h"
17+
18+
static struct nvs_fs fs;
19+
20+
#define NVS_PARTITION storage_partition
21+
#define NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(NVS_PARTITION)
22+
#define NVS_PARTITION_OFFSET FIXED_PARTITION_OFFSET(NVS_PARTITION)
23+
24+
/* Register a module for logging */
25+
LOG_MODULE_REGISTER(eeprom, LOG_LEVEL_INF);
26+
27+
int arduino::ZephyrEEPROM::nvs_init(void)
28+
{
29+
int rc = 0;
30+
31+
struct flash_pages_info info;
32+
33+
fs.flash_device = NVS_PARTITION_DEVICE;
34+
if (!device_is_ready(fs.flash_device)) {
35+
LOG_ERR("Flash device %s is not ready", fs.flash_device->name);
36+
return -ENODEV;
37+
}
38+
39+
fs.offset = NVS_PARTITION_OFFSET;
40+
41+
rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
42+
if (rc) {
43+
LOG_ERR("Unable to get page info");
44+
return rc;
45+
}
46+
47+
fs.sector_size = info.size;
48+
fs.sector_count = 3U;
49+
50+
rc = nvs_mount(&fs);
51+
if (rc) {
52+
LOG_ERR("Flash Init failed");
53+
return rc;
54+
}
55+
56+
LOG_INF("NVS initialized successfully");
57+
return 0;
58+
}
59+
60+
int arduino::ZephyrEEPROM::read_data(uint16_t id, void *data, size_t max_len)
61+
{
62+
/* Correctly pass data and max_len */
63+
int rc = nvs_read(&fs, id, data, max_len);
64+
if (rc > 0) {
65+
/* Data successfully read */
66+
return rc;
67+
} else if (rc == 0) {
68+
LOG_ERR("Data not found for ID: %d", id);
69+
} else {
70+
LOG_ERR("Error reading data for ID: %d, Error: %d", id, rc);
71+
}
72+
/* Return the result code, which can indicate an error or success */
73+
return rc;
74+
}
75+
76+
int arduino::ZephyrEEPROM::write_data(uint16_t id, const void *data, size_t data_len)
77+
{
78+
/* Pass data directly */
79+
int rc = nvs_write(&fs, id, data, data_len);
80+
if (rc < 0) {
81+
LOG_ERR("Error writing data for ID: %d, Error: %d", id, rc);
82+
}
83+
return rc;
84+
}
85+
86+
arduino::ZephyrEEPROM EEPROM;

libraries/EEPROM/EEPROM.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2025 Purva Yeshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <zephyr/kernel.h>
10+
#include <zephyr/fs/nvs.h>
11+
12+
#include <zephyr/device.h>
13+
#include <zephyr/drivers/flash.h>
14+
#include <zephyr/storage/flash_map.h>
15+
#include <string.h>
16+
#include <stdio.h>
17+
#include <zephyr/sys/reboot.h>
18+
19+
namespace arduino {
20+
21+
class ZephyrEEPROM {
22+
public:
23+
/* Constructor */
24+
ZephyrEEPROM() = default;
25+
26+
/* Initialize the NVS storage (mounts the NVS file system) */
27+
int nvs_init(void);
28+
29+
30+
/*
31+
* Write data to NVS
32+
*
33+
* Parameters:
34+
* - id: Unique identifier for the data
35+
* - data: Pointer to the data to write
36+
* - data_len: Length of the data to write
37+
*/
38+
int write_data(uint16_t id, const void *data, size_t data_len);
39+
40+
41+
/*
42+
* Read data from NVS
43+
*
44+
* Parameters:
45+
* - id: Unique identifier for the data
46+
* - data: Pointer to buffer where the data will be read into
47+
* - max_len: Maximum length of data to read
48+
*/
49+
int read_data(uint16_t id, void *data, size_t max_len);
50+
51+
52+
private:
53+
/* NVS file system structure used for managing flash memory */
54+
struct nvs_fs fs;
55+
};
56+
57+
}
58+
59+
extern arduino::ZephyrEEPROM EEPROM;
60+
61+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
6+
set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(eeprom_operations)
10+
11+
target_sources(app PRIVATE src/app.cpp)
12+
13+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/eeprom_operations/README.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. _eeprom_operations:
2+
3+
EEPROM Operations
4+
###############
5+
6+
Overview
7+
********
8+
9+
A sample that reads data from flash memory, as well as writes data to flash memory.

samples/eeprom_operations/prj.conf

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CONFIG_GPIO=y
2+
CONFIG_ARDUINO_API=y
3+
CONFIG_SPI=y
4+
CONFIG_EEPROM=yCONFIG_FLASH_PAGE_LAYOUT
5+
6+
CONFIG_PWM=y
7+
CONFIG_ADC=y
8+
CONFIG_GPIO_GET_DIRECTION=y
9+
10+
CONFIG_LOG=y
11+
CONFIG_MAIN_STACK_SIZE=8192
12+
CONFIG_ENTROPY_GENERATOR=y
13+
14+
CONFIG_NVS=y
15+
CONFIG_FLASH=y
16+
CONFIG_FLASH_MAP=y
17+
18+
CONFIG_LOG_MODE_IMMEDIATE=y
19+
CONFIG_NVS_LOG_LEVEL_DBG=y
20+
CONFIG_REBOOT=y
21+
CONFIG_MPU_ALLOW_FLASH_WRITE=y
22+
23+
CONFIG_FLASH_PAGE_LAYOUT=y

samples/eeprom_operations/src/app.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2025 Purva Yeshi <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
8+
#include <Arduino.h>
9+
#include "EEPROM.h"
10+
11+
void setup() {
12+
/* Initialize serial communication */
13+
Serial.begin(115200);
14+
while (!Serial) {
15+
16+
; /* Wait for serial port to connect (needed for boards with native USB) */
17+
}
18+
Serial.println("Serial communication initialized");
19+
20+
/* Initialize EEPROM/NVS */
21+
if (EEPROM.nvs_init() < 0) {
22+
Serial.println("NVS initialization failed");
23+
return;
24+
}
25+
Serial.println("NVS initialized");
26+
27+
/* Write data to EEPROM */
28+
int data = 1234; // Example data to store
29+
if (EEPROM.write_data(1, &data, sizeof(data)) < 0) {
30+
Serial.println("Failed to write data");
31+
} else {
32+
Serial.println("Data written successfully");
33+
}
34+
35+
/* Read data from EEPROM */
36+
int read_data = 0;
37+
if (EEPROM.read_data(1, &read_data, sizeof(read_data)) > 0) {
38+
Serial.print("Data read: ");
39+
Serial.println(read_data);
40+
} else {
41+
Serial.println("Failed to read data");
42+
}
43+
}
44+
45+
void loop() {
46+
47+
}

0 commit comments

Comments
 (0)