Skip to content

Commit

Permalink
fix #42 ReadObject, WriteObject (#43)
Browse files Browse the repository at this point in the history
- fix #42 ReadObject, WriteObject for FRAM32
- add examples
- minor edits
  • Loading branch information
RobTillaart authored Sep 17, 2023
1 parent 711800a commit 221e8dc
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 34 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.5.4] - 2023-09-16
- fix #42 ReadObject, WriteObject for FRAM32
- add examples
- minor edits

----

## [0.5.4] - 2023-09-04
- merge #39 add **read64(), write64()** (kudos to pvogt09)
- merge #40 add **MB85RC64V** info (kudos to pvogt09)
Expand All @@ -15,7 +22,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- improved readability parameter names
- update readme.md


## [0.5.3] - 2023-04-28
- major redo FRAM9 and FRAM11, see #35
- update FRAM32 to match FRAM
Expand Down
22 changes: 4 additions & 18 deletions FRAM.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: FRAM.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.4
// VERSION: 0.6.0
// DATE: 2018-01-24
// PURPOSE: Arduino library for I2C FRAM
// URL: https://github.com/RobTillaart/FRAM_I2C
Expand Down Expand Up @@ -346,12 +346,12 @@ void FRAM::sleep()


// page 12 datasheet trec <= 400us
bool FRAM::wakeup(uint32_t trec)
bool FRAM::wakeup(uint32_t timeRecover)
{
bool b = isConnected(); // wakeup
if (trec == 0) return b;
if (timeRecover == 0) return b;
// wait recovery time
delayMicroseconds(trec);
delayMicroseconds(timeRecover);
return isConnected(); // check recovery OK
}

Expand Down Expand Up @@ -586,20 +586,6 @@ int32_t FRAM32::readLine(uint32_t memAddr, char * buffer, uint16_t bufferLength)
}


template <class T> uint32_t FRAM32::writeObject(uint32_t memAddr, T &obj)
{
write(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
};


template <class T> uint32_t FRAM32::readObject(uint32_t memAddr, T &obj)
{
read(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
}


uint32_t FRAM32::clear(uint8_t value)
{
uint8_t buffer[16];
Expand Down
25 changes: 18 additions & 7 deletions FRAM.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: FRAM.h
// AUTHOR: Rob Tillaart
// VERSION: 0.5.4
// VERSION: 0.6.0
// DATE: 2018-01-24
// PURPOSE: Arduino library for I2C FRAM
// URL: https://github.com/RobTillaart/FRAM_I2C
Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define FRAM_LIB_VERSION (F("0.5.4"))
#define FRAM_LIB_VERSION (F("0.6.0"))


#define FRAM_OK 0
Expand Down Expand Up @@ -83,6 +83,7 @@ class FRAM
int32_t readLine(uint16_t memAddr, char * buffer, uint16_t bufferLength);


// template function must be in .h file
template <class T> uint16_t writeObject(uint16_t memAddr, T &obj)
{
write(memAddr, (uint8_t *) &obj, sizeof(obj));
Expand Down Expand Up @@ -117,11 +118,10 @@ class FRAM
void setSizeBytes(uint32_t value);



// SLEEP - 0.3.6
void sleep();
// trec <= 400us P12
bool wakeup(uint32_t trec = 400);
// timeRecover <= 400us see trec P12
bool wakeup(uint32_t timeRecover = 400);


protected:
Expand Down Expand Up @@ -173,8 +173,19 @@ class FRAM32 : public FRAM
// buffer needs one place for end char '\0'.
int32_t readLine(uint32_t memAddr, char * buffer, uint16_t bufferLength);

template <class T> uint32_t writeObject(uint32_t memAddr, T &obj);
template <class T> uint32_t readObject(uint32_t memAddr, T &obj);

// template function must be in .h file
template <class T> uint32_t writeObject(uint32_t memAddr, T &obj)
{
write(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
};

template <class T> uint32_t readObject(uint32_t memAddr, T &obj)
{
read(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
}

uint32_t clear(uint8_t value = 0); // fills FRAM with value

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ In the **FRAM32** class these functions have an **uin32_t memAddr**.
- **clear()** does not work for **MB85RC128A** unless **setSizeBytes()** is used.


(Template functions, see issue #13)
(Template functions, see issue #13, #42)
- **uint16_t writeObject(uint16_t memAddr, T &obj)** writes an object to memAddr (and following bytes).
- Returns memAddr + sizeof(obj) to get the next address to write to.
- **uint16_t readObject(uint16_t memAddr, T &obj)** reads an object from memAddr and next bytes.
Expand Down Expand Up @@ -177,7 +177,6 @@ Will work only if a writeProtectPin was defined in **begin()**
- Returns false if the writeProtectPin was not defined.



### Metadata

These may not work for devices that have no **deviceID** register.
Expand Down
90 changes: 90 additions & 0 deletions examples/FRAM11_writeObject/FRAM11_writeObject.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// FILE: FRAM11_writeObject.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo writing reading objects
// URL: https://github.com/RobTillaart/FRAM_I2C
//
// experimental


#include "FRAM.h"

FRAM11 fram;

uint32_t start;
uint32_t stop;

uint32_t sizeInBytes = 0;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("FRAM_LIB_VERSION: ");
Serial.println(FRAM_LIB_VERSION);

Wire.begin();

int rv = fram.begin(0x50);
if (rv != 0)
{
Serial.print("INIT ERROR: ");
Serial.println(rv);
}

// get size in bytes
sizeInBytes = fram.getSize() * 1024;
// clear FRAM
for (uint32_t addr = 0; addr < sizeInBytes; addr++)
{
fram.write8(addr, 0x00);
}

test_float();
test_struct();

}


void loop()
{
}


void test_float()
{
float x = 3.14159265;
Serial.println(x, 6);
fram.writeObject(100, x);

x = 1.0/x;
Serial.println(x, 6);

fram.readObject(100, x);
Serial.println(x, 6);
}

struct point
{
float x;
float y;
float z;
} P = {3.91, 5.24, 7.58};

void test_struct()
{
Serial.println(P.x);
Serial.println(P.y);
Serial.println(P.z);
fram.writeObject(50, P);

struct point Q;
fram.readObject(50, Q);
Serial.println(Q.x);
Serial.println(Q.y);
Serial.println(Q.z);
}


// -- END OF FILE --
6 changes: 3 additions & 3 deletions examples/FRAM32_Performance/FRAM32_Performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FRAM32 fram;
uint32_t start;
uint32_t stop;

int ar[600]; // 1200 bytes
int ar[600]; // 1200 bytes


void setup()
Expand All @@ -35,7 +35,7 @@ void setup()
}
else
{
for (int s = 1; s < 9; s++) // test up to 800 KB
for (int s = 1; s < 9; s++) // test up to 800 KB
{
uint32_t speed = s * 100000UL;
Serial.print("CLOCK: ");
Expand Down Expand Up @@ -94,4 +94,4 @@ void testReadWriteLarge()
}


// -- END OF FILE --
// -- END OF FILE --
90 changes: 90 additions & 0 deletions examples/FRAM32_writeObject/FRAM32_writeObject.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// FILE: FRAM32_writeObject.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo writing reading objects
// URL: https://github.com/RobTillaart/FRAM_I2C
//
// experimental


#include "FRAM.h"

FRAM32 fram;

uint32_t start;
uint32_t stop;

uint32_t sizeInBytes = 0;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("FRAM_LIB_VERSION: ");
Serial.println(FRAM_LIB_VERSION);

Wire.begin();

int rv = fram.begin(0x50);
if (rv != 0)
{
Serial.print("INIT ERROR: ");
Serial.println(rv);
}

// get size in bytes
sizeInBytes = fram.getSize() * 1024;
// clear FRAM
for (uint32_t addr = 0; addr < sizeInBytes; addr++)
{
fram.write8(addr, 0x00);
}

test_float();
test_struct();

}


void loop()
{
}


void test_float()
{
float x = 3.14159265;
Serial.println(x, 6);
fram.writeObject(100, x);

x = 1.0/x;
Serial.println(x, 6);

fram.readObject(100, x);
Serial.println(x, 6);
}

struct point
{
float x;
float y;
float z;
} P = {3.91, 5.24, 7.58};

void test_struct()
{
Serial.println(P.x);
Serial.println(P.y);
Serial.println(P.z);
fram.writeObject(50, P);

struct point Q;
fram.readObject(50, Q);
Serial.println(Q.x);
Serial.println(Q.y);
Serial.println(Q.z);
}


// -- END OF FILE --
2 changes: 1 addition & 1 deletion examples/testFRAMPerformance/testFRAMPerformance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FRAM fram;
uint32_t start;
uint32_t stop;

int ar[600]; // 1200 bytes
int ar[600]; // 1200 bytes


void setup()
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/FRAM_I2C.git"
},
"version": "0.5.4",
"version": "0.6.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=FRAM_I2C
version=0.5.4
version=0.6.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for I2C FRAM for persistent storage.
Expand Down

0 comments on commit 221e8dc

Please sign in to comment.