mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.3.3 FRAM_I2C
This commit is contained in:
parent
44be50e398
commit
4f7b4641cf
@ -1,27 +1,26 @@
|
||||
//
|
||||
// FILE: FRAM.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.2
|
||||
// VERSION: 0.3.3
|
||||
// DATE: 2018-01-24
|
||||
// PURPOSE: Arduino library for I2C FRAM
|
||||
// URL: https://github.com/RobTillaart/FRAM_I2C
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2018-01-24 initial version
|
||||
// 0.1.1 2019-07-31 added support for Fujitsu 64Kbit MB85RC64T (kudos ysoyipek)
|
||||
// 0.2.0 2020-04-30 refactor, add writeProtectPin code
|
||||
// 0.2.1 2020-06-10 fix library.json
|
||||
// 0.2.2 2020-12-23 Arduino-CI + unit test + getWriteProtect()
|
||||
// 0.2.3 2021-01-ii fix getMetaData (kudos to PraxisSoft
|
||||
// 0.3.0 2021-01-13 fix #2 ESP32 + WireN support
|
||||
// 0.3.1 2021-02-05 fix #7 typo in .cpp
|
||||
// 0.3.2 2021-12-18 update Arduino-CI, add badges,
|
||||
// update library.json, license, minor edits
|
||||
// HISTORY: see releaseNotes.md
|
||||
|
||||
|
||||
#include "FRAM.h"
|
||||
|
||||
|
||||
// DENSITY CODES
|
||||
|
||||
#define FRAM_MB85RC64 0x03
|
||||
#define FRAM_MB85RC256 0x05
|
||||
#define FRAM_MB85RC512 0x06
|
||||
#define FRAM_MB85RC1M 0x07
|
||||
|
||||
|
||||
// used for metadata
|
||||
const uint8_t FRAM_SLAVE_ID_= 0x7C;
|
||||
|
||||
|
||||
@ -196,22 +195,24 @@ uint16_t FRAM::getProductID()
|
||||
}
|
||||
|
||||
|
||||
// NOTE: returns the number of kiloBYTE
|
||||
uint16_t FRAM::getSize()
|
||||
{
|
||||
uint16_t val = getMetaData(2); // density bits
|
||||
if (val > 0) return 1UL << val;
|
||||
uint16_t density = getMetaData(2);
|
||||
if (density > 0) return 1UL << density;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
// metadata is packed as [....MMMM][MMMMDDDD][PPPPPPPP]
|
||||
// M = manufacturerID
|
||||
// D = density => memsize = 2^D KB
|
||||
// P = product ID (together with D)
|
||||
// metadata is packed as [....MMMM][MMMMDDDD][PPPPPPPP]
|
||||
// M = manufacturerID
|
||||
// D = density => memory size = 2^D KB
|
||||
// P = product ID (together with D)
|
||||
uint16_t FRAM::getMetaData(uint8_t field)
|
||||
{
|
||||
if (field > 2) return 0;
|
||||
@ -234,6 +235,10 @@ uint16_t FRAM::getMetaData(uint8_t field)
|
||||
// PRODUCT ID
|
||||
if (field == 1) return value & 0x0FFF;
|
||||
// DENSITY
|
||||
// 3 => MB85RC64
|
||||
// 5 => MB85RC256
|
||||
// 6 => MB85RC512
|
||||
// 7 => MB85RC1M
|
||||
if (field == 2) return (value >> 8) & 0x0F;
|
||||
return 0;
|
||||
}
|
||||
@ -268,4 +273,6 @@ void FRAM::readBlock(uint16_t memaddr, uint8_t * obj, uint8_t size)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: FRAM.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.2
|
||||
// VERSION: 0.3.3
|
||||
// DATE: 2018-01-24
|
||||
// PURPOSE: Arduino library for I2C FRAM
|
||||
// URL: https://github.com/RobTillaart/FRAM_I2C
|
||||
@ -13,7 +13,7 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define FRAM_LIB_VERSION (F("0.3.2"))
|
||||
#define FRAM_LIB_VERSION (F("0.3.3"))
|
||||
|
||||
|
||||
#define FRAM_OK 0
|
||||
@ -45,16 +45,22 @@ public:
|
||||
uint32_t read32(uint16_t memaddr);
|
||||
void read(uint16_t memaddr, uint8_t * obj, uint16_t size);
|
||||
|
||||
// works only if pin is defined in begin.
|
||||
bool setWriteProtect(bool b);
|
||||
bool getWriteProtect();
|
||||
|
||||
uint16_t getManufacturerID();
|
||||
uint16_t getProductID();
|
||||
uint16_t getSize();
|
||||
// meta info
|
||||
uint16_t getManufacturerID(); // Fujitsu = 0x000A
|
||||
uint16_t getProductID(); // Proprietary
|
||||
uint16_t getSize(); // Returns kiloBYTE
|
||||
uint32_t getSizeBytes() { return getSize() * 1024UL; };
|
||||
|
||||
|
||||
private:
|
||||
uint8_t _address;
|
||||
int8_t _writeProtectPin = -1; // default no pin ==> no write protect.
|
||||
|
||||
// default no pin = -1 ==> no write protect.
|
||||
int8_t _writeProtectPin = -1;
|
||||
|
||||
uint16_t getMetaData(uint8_t id);
|
||||
void writeBlock(uint16_t memaddr, uint8_t * obj, uint8_t size);
|
||||
|
@ -8,73 +8,98 @@
|
||||
|
||||
# FRAM_I2C
|
||||
|
||||
Arduino library for I2C FRAM
|
||||
Arduino library for I2C FRAM.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
FRAM is a library to read and write (over I2C) to an FRAM module.
|
||||
FRAM is a library to read from and write to (over I2C) an FRAM module.
|
||||
FRAM is much faster than EEPROM and almost as fast as Arduino UNO RAM.
|
||||
Another important feature is that FRAM keeps its content after a reboot (non-volatile)
|
||||
Another important feature it has in common with EEPROM is that FRAM keeps
|
||||
its content after a reboot (non-volatile).
|
||||
|
||||
FRAM stands for Ferroelectric RAM - https://en.wikipedia.org/wiki/Ferroelectric_RAM
|
||||
|
||||
Types of FRAM the library should work with
|
||||
|
||||
| SIZE | TYPE |
|
||||
|:-----:|:----------:|
|
||||
| 8KB | MB85RC64T |
|
||||
| 32KB | MB85RC256V |
|
||||
| 64KB | MB85RC512T |
|
||||
| 128KB | MB85RC1MT |
|
||||
| TYPE | SIZE | TESTED | NOTES |
|
||||
|:----------:|-------:|:------:|:---------------------|
|
||||
| MB85RC04 | 512 | | no deviceID register |
|
||||
| MB85RC16 | 2 KB | | no deviceID register |
|
||||
| MB85RC64T | 8 KB | Y |
|
||||
| MB85RC128A | 16 KB | | no deviceID register |
|
||||
| MB85RC256V | 32 KB | |
|
||||
| MB85RC512T | 64 KB | Y |
|
||||
| MB85RC1MT | 128 KB | |
|
||||
|
||||
Address = 0x50 (default) .. 0x57
|
||||
Notes
|
||||
- Not all types are tested. Pleas let me know if you have verified one.
|
||||
- If there is no deviceID **getSize()** will not work correctly.
|
||||
|
||||
Address = 0x50 (default) .. 0x57, depends on the lines A0..A2.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
|
||||
### Constructor
|
||||
|
||||
- **FRAM(TwoWire \*wire = &Wire)** Constructor with optional Wire interface.
|
||||
- **int begin(uint8_t address = 0x50, int8_t writeProtectPin = -1)** address and writeProtectPin is optional.
|
||||
- **int begin(uint8_t sda, uint8_t scl, uint8_t address = 0x50, int8_t writeProtectPin = -1)** idem fro ESP32 a.o.
|
||||
- **int begin(uint8_t sda, uint8_t scl, uint8_t address = 0x50, int8_t writeProtectPin = -1)** idem for ESP32 a.o.
|
||||
- **bool isConnected()** checks if the address is visible on the I2C bus.
|
||||
|
||||
|
||||
### Write & read
|
||||
|
||||
Support for basic types and 2 calls for generic object
|
||||
Support for basic types and 2 calls for generic object, use casting if needed.
|
||||
|
||||
- **void write8(uint16_t memaddr, uint8_t value)** uint8_t
|
||||
- **void write16(uint16_t memaddr, uint16_t value)** uint16_t
|
||||
- **void write32(uint16_t memaddr, uint32_t value)** uint32_t
|
||||
- **void write(uint16_t memaddr, uint8_t \* obj, uint16_t size)** other types / sizes
|
||||
- **void write(uint16_t memaddr, uint8_t \* obj, uint16_t size)** other types / sizes.
|
||||
- **uint8_t read8(uint16_t memaddr)**
|
||||
- **uint16_t read16(uint16_t memaddr)**
|
||||
- **uint32_t read32(uint16_t memaddr)**
|
||||
- **void read(uint16_t memaddr, uint8_t uint8_t \* obj, uint16_t size)**
|
||||
- **void read(uint16_t memaddr, uint8_t uint8_t \* obj, uint16_t size)**
|
||||
One needs to allocate memory as the function won't.
|
||||
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
- **bool setWriteProtect(bool b)** make the FRAM write-protected by pulling line HIGH / LOW.
|
||||
Returns true if a writeProtectPin was defined.
|
||||
- **bool setWriteProtect(bool b)** make the FRAM write-protected by pulling line HIGH / LOW.
|
||||
Returns true if a writeProtectPin was defined.
|
||||
Otherwise the FRAM cannot be write protected.
|
||||
- **bool getWriteProtect()** get current status.
|
||||
- **uint16_t getManufacturerID()** idem.
|
||||
- **uint16_t getProductID()** idem.
|
||||
- **uint16_t getSize()** returns size in KB.
|
||||
- **uint16_t getManufacturerID()** idem. Fujitsu = 0x00A.
|
||||
- **uint16_t getProductID()** idem. Proprietary.
|
||||
- **uint16_t getSize()** returns size in kiloBYTE.
|
||||
If the FRAM has no device ID, the size cannot be read.
|
||||
- **uint32_t getSizeBytes()** returns size in BYTES.
|
||||
Convenience wrapper, useful for iterating over the whole memory,
|
||||
or testing upper boundary.
|
||||
|
||||
|
||||
## Operational
|
||||
|
||||
|
||||
See examples
|
||||
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- test
|
||||
- test more types
|
||||
- new functionality
|
||||
- **clear(begin, end)** or complete clear / format only?
|
||||
- **dump(stream)** or printable interface?
|
||||
- Print interface? expensive in performance per char..
|
||||
- **getSize()** scanning FRAM like EEPROM library?
|
||||
- **write(addr obj size)** should return end + 1 == next address.
|
||||
- remember last written address? why?
|
||||
- extend examples
|
||||
- example in which FRAM is used as buffer for some stream?
|
||||
- example FRAM ring buffer
|
||||
-
|
||||
- FRAM for multi language string storage
|
||||
- FRAM as linear buffer for a slow stream?
|
||||
- FRAM as ring buffer
|
||||
- FRAM logging, unequal length strings.
|
||||
- FRAM (8x) concatenated as one continuous memory.
|
||||
|
||||
|
||||
|
77
libraries/FRAM_I2C/examples/FRAM_logging/FRAM_logging.ino
Normal file
77
libraries/FRAM_I2C/examples/FRAM_logging/FRAM_logging.ino
Normal file
@ -0,0 +1,77 @@
|
||||
//
|
||||
// FILE: FRAM_logging.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo persistant logging in FRAM.
|
||||
// URL: https://github.com/RobTillaart/FRAM_I2C
|
||||
|
||||
// experimental code
|
||||
// todo - read back sketch.
|
||||
//
|
||||
// last written position uint32_t at addres 0..3
|
||||
// log entry: plain text separated by newlines.
|
||||
// timestamp + \t + random number +\n
|
||||
// wraps around if FRAM full => might give corrupted one corrupted line.
|
||||
//
|
||||
|
||||
#include "FRAM.h"
|
||||
|
||||
FRAM 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);
|
||||
}
|
||||
fram.write32(0, 0x00000004);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
char buffer[24];
|
||||
|
||||
sprintf(buffer, "%ld\t%ld\n", millis(), random());
|
||||
Serial.print(buffer);
|
||||
log2fram(buffer);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
void log2fram(char * str)
|
||||
{
|
||||
uint32_t len = strlen(str);
|
||||
// might skip reading logaddr and make it static.
|
||||
uint32_t logaddr = fram.read32(0);
|
||||
if (logaddr + len > sizeInBytes) logaddr = 0x04; // start at begin
|
||||
|
||||
fram.write(logaddr, (uint8_t *)str, len);
|
||||
logaddr += len;
|
||||
fram.write32(0, logaddr);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -3,6 +3,7 @@
|
||||
# Data types (KEYWORD1)
|
||||
FRAM KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
begin KEYWORD2
|
||||
isConnected KEYWORD2
|
||||
@ -23,6 +24,7 @@ getWriteProtect KEYWORD2
|
||||
getManufacturerID KEYWORD2
|
||||
getProductID KEYWORD2
|
||||
getSize KEYWORD2
|
||||
getSizeBytes KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/FRAM_I2C.git"
|
||||
},
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.3",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=FRAM_I2C
|
||||
version=0.3.2
|
||||
version=0.3.3
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for I2C FRAM.
|
||||
|
46
libraries/FRAM_I2C/releaseNotes.md
Normal file
46
libraries/FRAM_I2C/releaseNotes.md
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
# FRAM I2C library
|
||||
|
||||
## Release notes
|
||||
|
||||
|
||||
## 0.3.3 2022-02-08
|
||||
- added **getSizeBytes()**
|
||||
- created releaseNotes.md
|
||||
- updated readme.md
|
||||
- add example FRAM_logging.ino
|
||||
|
||||
## 0.3.2 2021-12-18
|
||||
- update Arduino-CI,
|
||||
- add badges in readme.md
|
||||
- update library.json,
|
||||
- update license,
|
||||
- minor edits
|
||||
|
||||
## 0.3.1 2021-02-05
|
||||
- fix #7 typo in .cpp
|
||||
|
||||
## 0.3.0 2021-01-13
|
||||
- fix #2 ESP32
|
||||
- add WireN support
|
||||
|
||||
## 0.2.3 2021-01-11
|
||||
- fix getMetaData (kudos to PraxisSoft)
|
||||
|
||||
## 0.2.2 2020-12-23
|
||||
- add Arduino-CI + unit test
|
||||
- add **getWriteProtect()**
|
||||
|
||||
## 0.2.1 2020-06-10
|
||||
- fix library.json
|
||||
|
||||
## 0.2.0 2020-04-30
|
||||
- refactor,
|
||||
- add writeProtectPin code
|
||||
|
||||
## 0.1.1 2019-07-31
|
||||
- added support for Fujitsu 64Kbit MB85RC64T (kudos to ysoyipek)
|
||||
|
||||
## 0.1.0 2018-01-24
|
||||
- initial version
|
||||
|
Loading…
Reference in New Issue
Block a user