Added RTC Flash memory third party library

This commit is contained in:
anschrammh 2019-05-01 22:25:22 +02:00
parent 2dabcfa334
commit 111279603e
7 changed files with 689 additions and 0 deletions

View File

@ -0,0 +1,307 @@
/**
AT24CX.cpp
Library for using the EEPROM AT24C32/64
Copyright (c) 2014 Christian Paul
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "AT24CX.h"
#include <Wire.h>
/**
* Constructor with AT24Cx EEPROM at index 0
*/
AT24CX::AT24CX() {
init(0, 32);
}
/**
* Constructor with AT24Cx EEPROM at given index and size of page
*/
AT24CX::AT24CX(byte index, byte pageSize) {
init(index, pageSize);
}
/**
* Constructor with AT24C32 EEPROM at index 0
*/
AT24C32::AT24C32() {
init(0, 32);
}
/**
* Constructor with AT24Cx EEPROM at given index
*/
AT24C32::AT24C32(byte index) {
init(index, 32);
}
/**
* Constructor with AT24C64 EEPROM at index 0
*/
AT24C64::AT24C64() {
init(0, 32);
}
/**
* Constructor with AT24C64 EEPROM at given index
*/
AT24C64::AT24C64(byte index) {
init(index, 32);
}
/**
* Constructor with AT24C128 EEPROM at index 0
*/
AT24C128::AT24C128() {
init(0, 64);
}
/**
* Constructor with AT24C128 EEPROM at given index
*/
AT24C128::AT24C128(byte index) {
init(index, 64);
}
/**
* Constructor with AT24C256 EEPROM at index 0
*/
AT24C256::AT24C256() {
init(0, 64);
}
/**
* Constructor with AT24C128 EEPROM at given index
*/
AT24C256::AT24C256(byte index) {
init(index, 64);
}
/**
* Constructor with AT24C512 EEPROM at index 0
*/
AT24C512::AT24C512() {
init(0, 128);
}
/**
* Constructor with AT24C512 EEPROM at given index
*/
AT24C512::AT24C512(byte index) {
init(index, 128);
}
/**
* Init
*/
void AT24CX::init(byte index, byte pageSize) {
_id = AT24CX_ID | (index & 0x7);
_pageSize = pageSize;
Wire.begin();
}
/**
* Write byte
*/
void AT24CX::write(unsigned int address, byte data) {
Wire.beginTransmission(_id);
if(Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
Wire.write(data);
Wire.endTransmission();
delay(20);
}
}
/**
* Write integer
*/
void AT24CX::writeInt(unsigned int address, unsigned int data) {
write(address, (byte*)&data, 2);
}
/**
* Write long
*/
void AT24CX::writeLong(unsigned int address, unsigned long data) {
write(address, (byte*)&data, 4);
}
/**
* Write float
*/
void AT24CX::writeFloat(unsigned int address, float data) {
write(address, (byte*)&data, 4);
}
/**
* Write double
*/
void AT24CX::writeDouble(unsigned int address, double data) {
write(address, (byte*)&data, 8);
}
/**
* Write chars
*/
void AT24CX::writeChars(unsigned int address, char *data, int length) {
write(address, (byte*)data, length);
}
/**
* Read integer
*/
unsigned int AT24CX::readInt(unsigned int address) {
read(address, _b, 2);
return *(unsigned int*)&_b[0];
}
/**
* Read long
*/
unsigned long AT24CX::readLong(unsigned int address) {
read(address, _b, 4);
return *(unsigned long*)&_b[0];
}
/**
* Read float
*/
float AT24CX::readFloat(unsigned int address) {
read(address, _b, 4);
return *(float*)&_b[0];
}
/**
* Read double
*/
double AT24CX::readDouble(unsigned int address) {
read(address, _b, 8);
return *(double*)&_b[0];
}
/**
* Read chars
*/
void AT24CX::readChars(unsigned int address, char *data, int n) {
read(address, (byte*)data, n);
}
/**
* Write sequence of n bytes
*/
void AT24CX::write(unsigned int address, byte *data, int n) {
// status quo
int c = n; // bytes left to write
int offD = 0; // current offset in data pointer
int offP; // current offset in page
int nc = 0; // next n bytes to write
// write alle bytes in multiple steps
while (c > 0) {
// calc offset in page
offP = address % _pageSize;
// maximal 30 bytes to write
nc = min(min(c, 30), _pageSize - offP);
write(address, data, offD, nc);
c-=nc;
offD+=nc;
address+=nc;
}
}
/**
* Write sequence of n bytes from offset
*/
void AT24CX::write(unsigned int address, byte *data, int offset, int n) {
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
byte *adr = data+offset;
Wire.write(adr, n);
Wire.endTransmission();
delay(20);
}
}
/**
* Read byte
*/
byte AT24CX::read(unsigned int address) {
byte b = 0;
int r = 0;
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
Wire.requestFrom(_id, 1);
while (Wire.available() > 0 && r<1) {
b = (byte)Wire.read();
r++;
}
}
}
return b;
}
/**
* Read sequence of n bytes
*/
void AT24CX::read(unsigned int address, byte *data, int n) {
int c = n;
int offD = 0;
// read until are n bytes read
while (c > 0) {
// read maximal 32 bytes
int nc = c;
if (nc > 32)
nc = 32;
read(address, data, offD, nc);
address+=nc;
offD+=nc;
c-=nc;
}
}
/**
* Read sequence of n bytes to offset
*/
void AT24CX::read(unsigned int address, byte *data, int offset, int n) {
Wire.beginTransmission(_id);
if (Wire.endTransmission()==0) {
Wire.beginTransmission(_id);
Wire.write(address >> 8);
Wire.write(address & 0xFF);
if (Wire.endTransmission()==0) {
int r = 0;
Wire.requestFrom(_id, n);
while (Wire.available() > 0 && r<n) {
data[offset+r] = (byte)Wire.read();
r++;
}
}
}
}

View File

@ -0,0 +1,107 @@
/**
AT24CX.h
Library for using the EEPROM AT24C32/64
Copyright (c) 2014 Christian Paul
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef AT24CX_h
#define AT24CX_h
// includes
#include <Arduino.h>
// byte
typedef uint8_t byte;
// AT24Cx I2C adress
// 80
// 0x50
#define AT24CX_ID B1010000
// general class definition
class AT24CX {
public:
AT24CX();
AT24CX(byte index, byte pageSize);
void write(unsigned int address, byte data);
void write(unsigned int address, byte *data, int n);
void writeInt(unsigned int address, unsigned int data);
void writeLong(unsigned int address, unsigned long data);
void writeFloat(unsigned int address, float data);
void writeDouble(unsigned int address, double data);
void writeChars(unsigned int address, char *data, int length);
byte read(unsigned int address);
void read(unsigned int address, byte *data, int n);
unsigned int readInt(unsigned int address);
unsigned long readLong(unsigned int address);
float readFloat(unsigned int address);
double readDouble(unsigned int address);
void readChars(unsigned int address, char *data, int n);
protected:
void init(byte index, byte pageSize);
private:
void read(unsigned int address, byte *data, int offset, int n);
void write(unsigned int address, byte *data, int offset, int n);
int _id;
byte _b[8];
byte _pageSize;
};
// AT24C32 class definiton
class AT24C32 : public AT24CX {
public:
AT24C32();
AT24C32(byte index);
};
// AT24C64 class definiton
class AT24C64 : public AT24CX {
public:
AT24C64();
AT24C64(byte index);
};
// AT24C128 class definiton
class AT24C128 : public AT24CX {
public:
AT24C128();
AT24C128(byte index);
};
// AT24C256 class definiton
class AT24C256 : public AT24CX {
public:
AT24C256();
AT24C256(byte index);
};
// AT24C512 class definiton
class AT24C512 : public AT24CX {
public:
AT24C512();
AT24C512(byte index);
};
#endif

View File

@ -0,0 +1,128 @@
/*
*
* Read and write demo of the AT24CX library
* Written by Christian Paul, 2014-11-24
*
*
*/
// include libraries
#include <Wire.h>
#include <AT24CX.h>
// EEPROM object
AT24CX mem;
// setup
void setup() {
// serial init
Serial.begin(115200);
Serial.println("AT24CX read/write demo");
Serial.println("----------------------");
}
// main loop
void loop() {
// read and write byte
Serial.println("Write 42 to address 12");
mem.write(12, 42);
Serial.println("Read byte from address 12 ...");
byte b = mem.read(12);
Serial.print("... read: ");
Serial.println(b, DEC);
Serial.println();
// read and write integer
Serial.println("Write 65000 to address 15");
mem.writeInt(15, 65000);
Serial.println("Read integer from address 15 ...");
unsigned int i = mem.readInt(15);
Serial.print("... read: ");
Serial.println(i, DEC);
Serial.println();
// read and write long
Serial.println("Write 3293732729 to address 20");
mem.writeLong(20, 3293732729UL);
Serial.println("Read long from address 20 ...");
unsigned long l = mem.readLong(20);
Serial.print("... read: ");
Serial.println(l, DEC);
Serial.println();
// read and write long
Serial.println("Write 1111111111 to address 31");
mem.writeLong(31, 1111111111);
Serial.println("Read long from address 31 ...");
unsigned long l2 = mem.readLong(31);
Serial.print("... read: ");
Serial.println(l2, DEC);
Serial.println();
// read and write float
Serial.println("Write 3.14 to address 40");
mem.writeFloat(40, 3.14);
Serial.println("Read float from address 40 ...");
float f = mem.readFloat(40);
Serial.print("... read: ");
Serial.println(f, DEC);
Serial.println();
// read and write double
Serial.println("Write 3.14159265359 to address 50");
mem.writeDouble(50, 3.14159265359);
Serial.println("Read double from address 50 ...");
double d = mem.readDouble(50);
Serial.print("... read: ");
Serial.println(d, DEC);
Serial.println();
// read and write char
Serial.print("Write chars: '");
char msg[] = "This is a message";
Serial.print(msg);
Serial.println("' to address 200");
mem.writeChars(200, msg, sizeof(msg));
Serial.println("Read chars from address 200 ...");
char msg2[30];
mem.readChars(200, msg2, sizeof(msg2));
Serial.print("... read: '");
Serial.print(msg2);
Serial.println("'");
Serial.println();
// write array of bytes
Serial.println("Write array of 80 bytes at address 1000");
byte xy[] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9, // 10 x 3 = 30
10,11,12,13,14,15,16,17,18,19, // 10
120,121,122,123,124,125,126,127,128,129, // 10
130,131,132,133,134,135,136,137,138,139, // 10
200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219}; // 20
mem.write(1000, (byte*)xy, sizeof(xy));
// read bytes with multiple steps
Serial.println("Read 80 single bytes starting at address 1000");
for (int i=0; i<sizeof(xy); i++) {
byte sb = mem.read(1000+i);
Serial.print("[");
Serial.print(1000+i);
Serial.print("] = ");
Serial.println(sb);
}
Serial.println();
// read bytes with one step
Serial.println("Read 80 bytes with one operation at address 1000");
byte z[80];
memset(&z[0], 32, sizeof(z));
mem.read(1000, z, sizeof(z));
for (int i=0; i<sizeof(z); i++) {
Serial.print("[");
Serial.print(1000+i);
Serial.print("] = ");
Serial.println(z[i]);
}
// stop
while (1==1) {}
}

View File

@ -0,0 +1,40 @@
/*
*
* Search for an EEPROM.
* This sketch iterate the eight possible I2C addresses and
* checks if an EEPROM is found.
*
* Written by Christian Paul, 2014-11-24
*
*
*/
// include libraries
#include <Wire.h>
// setup
void setup() {
Serial.begin(115200);
Serial.println("AT24CX search");
Serial.println("-------------------------");
Wire.begin();
int i2c = 0x50;
for (int i=0; i<8; i++) {
Serial.print("Search at [");
Serial.print(i2c, HEX);
Serial.print("]: ");
Wire.beginTransmission(i2c);
int result = Wire.endTransmission();
if (result==0)
Serial.println("FOUND!");
else
Serial.println("not found");
i2c++;
}
}
// main loop
void loop() {}

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014-2015 Christian Paul
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,25 @@
The MIT License (MIT)
Copyright (c) 2014-2015 Christian Paul
----------
Übersetzung von Wikipedia - http://de.wikipedia.org/wiki/MIT-Lizenz
----
Hiermit wird unentgeltlich jeder Person, die eine Kopie der Software und der zugehörigen
Dokumentationen (die "Software") erhält, die Erlaubnis erteilt, sie uneingeschränkt zu
benutzen, inklusive und ohne Ausnahme dem Recht, sie zu verwenden, kopieren, ändern,
fusionieren, verlegen, verbreiten, unterlizenzieren und/oder zu verkaufen, und Personen, die
diese Software erhalten, diese Rechte zu geben, unter den folgenden Bedingungen:
Der obige Urheberrechtsvermerk und dieser Erlaubnisvermerk sind in allen Kopien oder
Teilkopien der Software beizulegen.
DIE SOFTWARE WIRD OHNE JEDE AUSDRÜCKLICHE ODER IMPLIZIERTE GARANTIE
BEREITGESTELLT, EINSCHLIESSLICH DER GARANTIE ZUR BENUTZUNG FÜR DEN
VORGESEHENEN ODER EINEM BESTIMMTEN ZWECK SOWIE JEGLICHER
RECHTSVERLETZUNG, JEDOCH NICHT DARAUF BESCHRÄNKT. IN KEINEM FALL SIND DIE
AUTOREN ODER COPYRIGHTINHABER FÜR JEGLICHEN SCHADEN ODER SONSTIGE
ANSPRÜCHE HAFTBAR ZU MACHEN, OB INFOLGE DER ERFÜLLUNG EINES VERTRAGES,
EINES DELIKTES ODER ANDERS IM ZUSAMMENHANG MIT DER SOFTWARE ODER
SONSTIGER VERWENDUNG DER SOFTWARE ENTSTANDEN.

View File

@ -0,0 +1,61 @@
# AT24CX Library
Library for using the Atmels EEPROM AT24C32/AT24C64/AT24C128/AT24C256/AT24C512 in Arduino projects.
See <https://oberguru.net/elektronik/eeprom/at24cx-at24c32-at24c64-at24c128-at24c256-at24c512.html> for definitons and differences.
Written by Christian Paul, 2014 - 2015.
This software is released under the terms of the MIT license.
See the file LICENSE or LIZENZ for details, please.
You can use any of the eight possibles EEPROM devices on the I2C bus.
Constructor
AT24CX(byte pageSize);
uses the device with index 0 and given page size. You can select a device with given index between 0 and 8 with constructor
AT24CX(byte index, byte pageSize);
Than, you can single write or read single bytes from the EEPROM with
void write(unsigned int address, byte data);
byte read(unsigned int address);
or write and read an array of bytes with
void write(unsigned int address, byte *data, int n);
void read(unsigned int address, byte *data, int n);
For writing integers, long, float, double or sequences of chars you can use the comfort functions
void writeInt(unsigned int address, unsigned int data);
void writeLong(unsigned int address, unsigned long data);
void writeFloat(unsigned int address, float data);
void writeDouble(unsigned int address, double data);
void writeChars(unsigned int address, char *data, int length);
Reading the values is done by using
unsigned int readInt(unsigned int address);
unsigned long readLong(unsigned int address);
float readFloat(unsigned int address);
double readDouble(unsigned int address);
void readChars(unsigned int address, char *data, int n);
Alternative you can use the individual classes with predefined page sizes:
AT24C32();
AT24C64();
AT24C128();
AT24C256();
AT24C512();
or with different index than 0:
AT24C32(byte index);
AT24C64(byte index);
AT24C128(byte index);
AT24C256(byte index);
AT24C512(byte index);