From 1d030d6f8567b05691c8eb1650c8808f7d625165 Mon Sep 17 00:00:00 2001
From: Purva Yeshi <purvayeshi550@gmail.com>
Date: Mon, 27 Jan 2025 19:54:13 +0530
Subject: [PATCH 1/3] Add EEPROM library

---
 libraries/CMakeLists.txt                 |  2 +
 libraries/EEPROM/CMakeLists.txt          |  6 ++
 libraries/EEPROM/EEPROM.cpp              | 86 ++++++++++++++++++++++++
 libraries/EEPROM/EEPROM.h                | 61 +++++++++++++++++
 samples/eeprom_operations/CMakeLists.txt | 13 ++++
 samples/eeprom_operations/README.rst     |  9 +++
 samples/eeprom_operations/prj.conf       | 23 +++++++
 samples/eeprom_operations/src/app.cpp    | 47 +++++++++++++
 8 files changed, 247 insertions(+)
 create mode 100644 libraries/EEPROM/CMakeLists.txt
 create mode 100644 libraries/EEPROM/EEPROM.cpp
 create mode 100644 libraries/EEPROM/EEPROM.h
 create mode 100644 samples/eeprom_operations/CMakeLists.txt
 create mode 100644 samples/eeprom_operations/README.rst
 create mode 100644 samples/eeprom_operations/prj.conf
 create mode 100644 samples/eeprom_operations/src/app.cpp

diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt
index 5600b327..0233d824 100644
--- a/libraries/CMakeLists.txt
+++ b/libraries/CMakeLists.txt
@@ -2,3 +2,5 @@
 
 add_subdirectory(Wire)
 add_subdirectory(SPI)
+add_subdirectory(EEPROM)
+
diff --git a/libraries/EEPROM/CMakeLists.txt b/libraries/EEPROM/CMakeLists.txt
new file mode 100644
index 00000000..34d0c014
--- /dev/null
+++ b/libraries/EEPROM/CMakeLists.txt
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: Apache-2.0
+zephyr_include_directories(.)
+
+if(NOT DEFINED ARDUINO_BUILD_PATH)
+	zephyr_sources_ifdef(CONFIG_EEPROM EEPROM.cpp)
+endif()
diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp
new file mode 100644
index 00000000..3ee56c5a
--- /dev/null
+++ b/libraries/EEPROM/EEPROM.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2024 Purva Yeshi <purvayeshi550@gmail.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <zephyr/kernel.h>
+#include <zephyr/sys/reboot.h>
+#include <zephyr/device.h>
+#include <string.h>
+#include <zephyr/drivers/flash.h>
+#include <zephyr/storage/flash_map.h>
+#include <zephyr/fs/nvs.h>
+#include <zephyr/logging/log.h>
+
+#include "EEPROM.h"
+
+static struct nvs_fs fs;
+
+#define NVS_PARTITION        storage_partition
+#define NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(NVS_PARTITION)
+#define NVS_PARTITION_OFFSET FIXED_PARTITION_OFFSET(NVS_PARTITION)
+
+/* Register a module for logging */
+LOG_MODULE_REGISTER(eeprom, LOG_LEVEL_INF);    
+
+int arduino::ZephyrEEPROM::nvs_init(void)
+{
+  int rc = 0;
+  
+  struct flash_pages_info info;
+
+  fs.flash_device = NVS_PARTITION_DEVICE;
+  if (!device_is_ready(fs.flash_device)) {
+    LOG_ERR("Flash device %s is not ready", fs.flash_device->name);
+     return -ENODEV;
+  }
+
+  fs.offset = NVS_PARTITION_OFFSET;
+
+  rc = flash_get_page_info_by_offs(fs.flash_device, fs.offset, &info);
+  if (rc) {
+    LOG_ERR("Unable to get page info");
+    return rc;
+  }
+
+  fs.sector_size = info.size;
+  fs.sector_count = 3U;
+
+  rc = nvs_mount(&fs);
+  if (rc) {
+    LOG_ERR("Flash Init failed");
+    return rc;
+  }
+
+  LOG_INF("NVS initialized successfully");
+  return 0;
+}
+
+int arduino::ZephyrEEPROM::read_data(uint16_t id, void *data, size_t max_len)
+{
+  /* Correctly pass data and max_len */ 
+  int rc = nvs_read(&fs, id, data, max_len); 
+  if (rc > 0) {
+    /* Data successfully read */
+    return rc; 
+  } else if (rc == 0) {
+    LOG_ERR("Data not found for ID: %d", id);
+  } else {
+    LOG_ERR("Error reading data for ID: %d, Error: %d", id, rc);
+  }
+  /* Return the result code, which can indicate an error or success */
+  return rc; 
+}
+
+int arduino::ZephyrEEPROM::write_data(uint16_t id, const void *data, size_t data_len)
+{
+  /* Pass data directly */
+  int rc = nvs_write(&fs, id, data, data_len); 
+  if (rc < 0) {
+    LOG_ERR("Error writing data for ID: %d, Error: %d", id, rc);
+  }
+  return rc;
+}
+
+arduino::ZephyrEEPROM EEPROM;
diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h
new file mode 100644
index 00000000..e5e136a6
--- /dev/null
+++ b/libraries/EEPROM/EEPROM.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2025 Purva Yeshi <purvayeshi550@gmail.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#pragma once
+
+#include <zephyr/kernel.h>
+#include <zephyr/fs/nvs.h>
+
+#include <zephyr/device.h>
+#include <zephyr/drivers/flash.h>
+#include <zephyr/storage/flash_map.h>
+#include <string.h>
+#include <stdio.h>
+#include <zephyr/sys/reboot.h>
+
+namespace arduino {
+
+class ZephyrEEPROM {
+public:
+    /* Constructor */
+    ZephyrEEPROM() = default;
+
+    /* Initialize the NVS storage (mounts the NVS file system) */
+    int nvs_init(void);
+
+
+    /*
+     * Write data to NVS
+     * 
+     * Parameters:
+     *   - id: Unique identifier for the data
+     *   - data: Pointer to the data to write
+     *   - data_len: Length of the data to write
+     */
+    int write_data(uint16_t id, const void *data, size_t data_len);
+
+
+    /* 
+     * Read data from NVS
+     *
+     * Parameters:
+     *   - id: Unique identifier for the data
+     *   - data: Pointer to buffer where the data will be read into
+     *   - max_len: Maximum length of data to read
+     */
+    int read_data(uint16_t id, void *data, size_t max_len);
+
+
+private:
+    /* NVS file system structure used for managing flash memory */ 
+    struct nvs_fs fs;
+};
+
+}  
+
+extern arduino::ZephyrEEPROM EEPROM;
+
+    
\ No newline at end of file
diff --git a/samples/eeprom_operations/CMakeLists.txt b/samples/eeprom_operations/CMakeLists.txt
new file mode 100644
index 00000000..e9217fd3
--- /dev/null
+++ b/samples/eeprom_operations/CMakeLists.txt
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: Apache-2.0
+
+cmake_minimum_required(VERSION 3.20.0)
+
+cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
+set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
+
+find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
+project(eeprom_operations)
+
+target_sources(app PRIVATE src/app.cpp)
+
+zephyr_compile_options(-Wno-unused-variable -Wno-comment)
diff --git a/samples/eeprom_operations/README.rst b/samples/eeprom_operations/README.rst
new file mode 100644
index 00000000..25f62c0a
--- /dev/null
+++ b/samples/eeprom_operations/README.rst
@@ -0,0 +1,9 @@
+.. _eeprom_operations:
+
+EEPROM Operations
+###############
+
+Overview
+********
+
+A sample that reads data from flash memory, as well as writes data to flash memory.
diff --git a/samples/eeprom_operations/prj.conf b/samples/eeprom_operations/prj.conf
new file mode 100644
index 00000000..8e4db619
--- /dev/null
+++ b/samples/eeprom_operations/prj.conf
@@ -0,0 +1,23 @@
+CONFIG_GPIO=y
+CONFIG_ARDUINO_API=y
+CONFIG_SPI=y
+CONFIG_EEPROM=yCONFIG_FLASH_PAGE_LAYOUT
+
+CONFIG_PWM=y
+CONFIG_ADC=y
+CONFIG_GPIO_GET_DIRECTION=y
+
+CONFIG_LOG=y
+CONFIG_MAIN_STACK_SIZE=8192
+CONFIG_ENTROPY_GENERATOR=y
+
+CONFIG_NVS=y
+CONFIG_FLASH=y
+CONFIG_FLASH_MAP=y
+
+CONFIG_LOG_MODE_IMMEDIATE=y
+CONFIG_NVS_LOG_LEVEL_DBG=y
+CONFIG_REBOOT=y
+CONFIG_MPU_ALLOW_FLASH_WRITE=y
+
+CONFIG_FLASH_PAGE_LAYOUT=y
diff --git a/samples/eeprom_operations/src/app.cpp b/samples/eeprom_operations/src/app.cpp
new file mode 100644
index 00000000..ed1c99f9
--- /dev/null
+++ b/samples/eeprom_operations/src/app.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2025 Purva Yeshi <purvayeshi550@gmail.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+
+#include <Arduino.h>
+#include "EEPROM.h" 
+
+void setup() {
+  /* Initialize serial communication */
+  Serial.begin(115200);
+  while (!Serial) {
+
+    ; /* Wait for serial port to connect (needed for boards with native USB) */
+  }
+  Serial.println("Serial communication initialized");
+
+  /* Initialize EEPROM/NVS */ 
+  if (EEPROM.nvs_init() < 0) { 
+    Serial.println("NVS initialization failed");
+    return;
+  }
+  Serial.println("NVS initialized");
+
+  /* Write data to EEPROM */
+  int data = 1234;  // Example data to store
+  if (EEPROM.write_data(1, &data, sizeof(data)) < 0) { 
+    Serial.println("Failed to write data");
+  } else {
+    Serial.println("Data written successfully");
+  }
+
+  /* Read data from EEPROM */
+  int read_data = 0;
+  if (EEPROM.read_data(1, &read_data, sizeof(read_data)) > 0) { 
+    Serial.print("Data read: ");
+    Serial.println(read_data);
+  } else {
+    Serial.println("Failed to read data");
+  }
+}
+
+void loop() {
+
+}

From 864b7a79de422df62b17391fa3ddd6cacf9fb36e Mon Sep 17 00:00:00 2001
From: Purva Yeshi <purvayeshi550@gmail.com>
Date: Thu, 30 Jan 2025 19:58:29 +0530
Subject: [PATCH 2/3] Improve code formatting and documentation

---
 libraries/EEPROM/EEPROM.h             | 57 +++++++++++++--------------
 samples/eeprom_operations/README.rst  | 55 +++++++++++++++++++++++++-
 samples/eeprom_operations/src/app.cpp |  8 ++--
 3 files changed, 84 insertions(+), 36 deletions(-)

diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h
index e5e136a6..fbe552ad 100644
--- a/libraries/EEPROM/EEPROM.h
+++ b/libraries/EEPROM/EEPROM.h
@@ -20,38 +20,35 @@ namespace arduino {
 
 class ZephyrEEPROM {
 public:
-    /* Constructor */
-    ZephyrEEPROM() = default;
-
-    /* Initialize the NVS storage (mounts the NVS file system) */
-    int nvs_init(void);
-
-
-    /*
-     * Write data to NVS
-     * 
-     * Parameters:
-     *   - id: Unique identifier for the data
-     *   - data: Pointer to the data to write
-     *   - data_len: Length of the data to write
-     */
-    int write_data(uint16_t id, const void *data, size_t data_len);
-
-
-    /* 
-     * Read data from NVS
-     *
-     * Parameters:
-     *   - id: Unique identifier for the data
-     *   - data: Pointer to buffer where the data will be read into
-     *   - max_len: Maximum length of data to read
-     */
-    int read_data(uint16_t id, void *data, size_t max_len);
-
+  /* Constructor */
+  ZephyrEEPROM() = default;
+
+  /* Initialize the NVS storage (mounts the NVS file system) */
+  int nvs_init(void);
+
+  /*
+   * Write data to NVS
+   * 
+   * Parameters:
+   *   - id: Unique identifier for the data
+   *   - data: Pointer to the data to write
+   *   - data_len: Length of the data to write
+   */
+  int write_data(uint16_t id, const void *data, size_t data_len);
+
+  /* 
+   * Read data from NVS
+   *
+   * Parameters:
+   *   - id: Unique identifier for the data
+   *   - data: Pointer to buffer where the data will be read into
+   *   - max_len: Maximum length of data to read
+   */
+  int read_data(uint16_t id, void *data, size_t max_len);
 
 private:
-    /* NVS file system structure used for managing flash memory */ 
-    struct nvs_fs fs;
+  /* NVS file system structure used for managing flash memory */ 
+  struct nvs_fs fs;
 };
 
 }  
diff --git a/samples/eeprom_operations/README.rst b/samples/eeprom_operations/README.rst
index 25f62c0a..9d378fb2 100644
--- a/samples/eeprom_operations/README.rst
+++ b/samples/eeprom_operations/README.rst
@@ -1,9 +1,60 @@
 .. _eeprom_operations:
 
+.. _eeprom_operations:
+
 EEPROM Operations
-###############
+#################
 
 Overview
 ********
 
-A sample that reads data from flash memory, as well as writes data to flash memory.
+Read and write data from flash memory using this sample.
+
+Prerequisites and Setup
+***********************
+
+Use any board that supports Zephyr RTOS.  
+This example uses **BeagleConnect Freedom**.
+
+For setup, refer to the `documentation <https://docs.beagle.cc/boards/beagleconnect/freedom/demos-and-tutorials/using-arduino-zephyr-template.html#setup-arduino-workspace>`_.
+
+Build and Test
+**************
+
+Follow these steps to set up and run the sample in the `arduino-workspace` directory:
+
+1. **Set up the virtual environment:**
+
+   .. code-block:: bash
+
+      source .venv/bin/activate
+
+2. **Build the EEPROM operations sample:**
+
+   .. code-block:: bash
+
+      west build -b beagleconnect_freedom modules/lib/Arduino-Zephyr-API/samples/eeprom_operations -p
+
+3. **Flash the code after connecting BeagleConnect Freedom to your device:**
+
+   .. code-block:: bash
+
+      west flash
+
+4. **Open the serial console to view the output:**
+
+   .. code-block:: bash
+
+      tio /dev/ttyACM0
+
+Sample Output
+*************
+
+Run the code and observe the following output:
+
+.. code-block:: text
+
+    Serial communication initialized
+    NVS initialized
+    Data written successfully
+    Data read: 1234
diff --git a/samples/eeprom_operations/src/app.cpp b/samples/eeprom_operations/src/app.cpp
index ed1c99f9..14ada3c6 100644
--- a/samples/eeprom_operations/src/app.cpp
+++ b/samples/eeprom_operations/src/app.cpp
@@ -4,15 +4,17 @@
  * SPDX-License-Identifier: Apache-2.0
  */
 
-
 #include <Arduino.h>
 #include "EEPROM.h" 
 
 void setup() {
+
+  int data = 1234;  // Example data to store
+  int read_data = 0;
+
   /* Initialize serial communication */
   Serial.begin(115200);
   while (!Serial) {
-
     ; /* Wait for serial port to connect (needed for boards with native USB) */
   }
   Serial.println("Serial communication initialized");
@@ -25,7 +27,6 @@ void setup() {
   Serial.println("NVS initialized");
 
   /* Write data to EEPROM */
-  int data = 1234;  // Example data to store
   if (EEPROM.write_data(1, &data, sizeof(data)) < 0) { 
     Serial.println("Failed to write data");
   } else {
@@ -33,7 +34,6 @@ void setup() {
   }
 
   /* Read data from EEPROM */
-  int read_data = 0;
   if (EEPROM.read_data(1, &read_data, sizeof(read_data)) > 0) { 
     Serial.print("Data read: ");
     Serial.println(read_data);

From 748e93b09b8f1f05781291ff9ee6b399361327f6 Mon Sep 17 00:00:00 2001
From: Purva Yeshi <purvayeshi550@gmail.com>
Date: Fri, 31 Jan 2025 00:56:46 +0530
Subject: [PATCH 3/3] Improve documentation

---
 samples/eeprom_operations/README.rst | 2 --
 1 file changed, 2 deletions(-)

diff --git a/samples/eeprom_operations/README.rst b/samples/eeprom_operations/README.rst
index 9d378fb2..58c8d29d 100644
--- a/samples/eeprom_operations/README.rst
+++ b/samples/eeprom_operations/README.rst
@@ -1,7 +1,5 @@
 .. _eeprom_operations:
 
-.. _eeprom_operations:
-
 EEPROM Operations
 #################