Skip to content

Commit

Permalink
Improve code formatting and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Purva Yeshi committed Jan 30, 2025
1 parent 1d030d6 commit 864b7a7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
57 changes: 27 additions & 30 deletions libraries/EEPROM/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

}
Expand Down
55 changes: 53 additions & 2 deletions samples/eeprom_operations/README.rst
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions samples/eeprom_operations/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -25,15 +27,13 @@ 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 {
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);
Expand Down

0 comments on commit 864b7a7

Please sign in to comment.