GY-63_MS5611/libraries/FRAM_I2C/FRAM.h

243 lines
6.9 KiB
C
Raw Normal View History

2021-01-29 06:31:58 -05:00
#pragma once
//
// FILE: FRAM.h
// AUTHOR: Rob Tillaart
2023-10-30 12:08:58 -04:00
// VERSION: 0.7.0
2021-01-29 06:31:58 -05:00
// DATE: 2018-01-24
// PURPOSE: Arduino library for I2C FRAM
// URL: https://github.com/RobTillaart/FRAM_I2C
#include "Arduino.h"
#include "Wire.h"
2023-10-30 12:08:58 -04:00
#define FRAM_LIB_VERSION (F("0.7.0"))
2021-01-29 06:31:58 -05:00
2023-02-26 09:02:10 -05:00
#define FRAM_OK 0
2021-01-29 06:31:58 -05:00
#define FRAM_ERROR_ADDR -10
#define FRAM_ERROR_I2C -11
#define FRAM_ERROR_CONNECT -12
2023-02-26 09:02:10 -05:00
// Size known types (Fujitsu)
#define FRAM_MB85RC04 512
2022-03-25 05:04:48 -04:00
#define FRAM_MB85RC16 2048
#define FRAM_MB85RC64T 8192
2023-09-07 08:07:49 -04:00
#define FRAM_MB85RC64V 8192
2022-03-25 05:04:48 -04:00
#define FRAM_MB85RC128A 16384
#define FRAM_MB85RC256V 32768
#define FRAM_MB85RC512T 65536
#define FRAM_MB85RC1MT 131072
2021-01-29 06:31:58 -05:00
class FRAM
{
public:
FRAM(TwoWire *wire = &Wire);
2023-02-26 09:02:10 -05:00
// address and writeProtectPin is optional
2023-10-30 12:08:58 -04:00
// user has to call Wire.begin() before FRAM.begin().
2023-02-26 09:02:10 -05:00
int begin(const uint8_t address = 0x50,
2022-09-25 04:04:25 -04:00
const int8_t writeProtectPin = -1);
2021-01-29 06:31:58 -05:00
bool isConnected();
2023-09-07 08:07:49 -04:00
// WRITE DATA TYPES
void write8(uint16_t memAddr, uint8_t value);
void write16(uint16_t memAddr, uint16_t value);
void write32(uint16_t memAddr, uint32_t value);
void write64(uint16_t memAddr, uint64_t value);
void writeFloat(uint16_t memAddr, float value);
void writeDouble(uint16_t memAddr, double value);
void write(uint16_t memAddr, uint8_t * obj, uint16_t size);
// fills FRAM with value, default 0.
uint32_t clear(uint8_t value = 0);
// READ DATA TYPES
uint8_t read8(uint16_t memAddr);
uint16_t read16(uint16_t memAddr);
uint32_t read32(uint16_t memAddr);
uint64_t read64(uint16_t memAddr);
float readFloat(uint16_t memAddr);
double readDouble(uint16_t memAddr);
void read(uint16_t memAddr, uint8_t * obj, uint16_t size);
2021-01-29 06:31:58 -05:00
2023-04-22 05:34:55 -04:00
// Experimental 0.5.1
// readUntil returns length 0.. n of the buffer.
2023-05-01 10:00:11 -04:00
// readUntil does NOT include the separator character.
2023-04-22 05:34:55 -04:00
// readUntil returns -1 if data does not fit into buffer,
// => separator not encountered.
2023-09-07 08:07:49 -04:00
int32_t readUntil(uint16_t memAddr, char * buffer, uint16_t bufferLength, char separator);
2023-04-22 05:34:55 -04:00
// readLine returns length 0.. n of the buffer.
2023-05-01 10:00:11 -04:00
// readLine does include '\n' as end character.
2023-04-22 05:34:55 -04:00
// readLine returns -1 if data does not fit into buffer.
// buffer needs one place for end char '\0'.
2023-09-07 08:07:49 -04:00
int32_t readLine(uint16_t memAddr, char * buffer, uint16_t bufferLength);
2023-04-22 05:34:55 -04:00
2023-09-17 04:13:35 -04:00
// template function must be in .h file
2023-09-07 08:07:49 -04:00
template <class T> uint16_t writeObject(uint16_t memAddr, T &obj)
2022-03-18 08:37:57 -04:00
{
2023-09-07 08:07:49 -04:00
write(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
2022-03-18 08:37:57 -04:00
};
2023-09-07 08:07:49 -04:00
template <class T> uint16_t readObject(uint16_t memAddr, T &obj)
2022-03-18 08:37:57 -04:00
{
2023-09-07 08:07:49 -04:00
read(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
2022-03-18 08:37:57 -04:00
}
2023-09-07 08:07:49 -04:00
// WRITEPROTECT
2023-05-01 10:00:11 -04:00
// works only if pin is defined in begin().
2021-01-29 06:31:58 -05:00
bool setWriteProtect(bool b);
bool getWriteProtect();
2023-09-07 08:07:49 -04:00
// MEAT INFO
// may not work for devices with no deviceID register.
2023-05-01 10:00:11 -04:00
// Fujitsu = 0x000A, Ramtron = 0x004
2023-02-26 09:02:10 -05:00
uint16_t getManufacturerID();
2023-09-07 08:07:49 -04:00
// Proprietary product ID
2023-02-26 09:02:10 -05:00
uint16_t getProductID();
2023-05-01 10:00:11 -04:00
// virtual so derived classes FRAM9/11 use their implementation.
2023-02-26 09:02:10 -05:00
// Returns size in KILO-BYTE (or 0)
2023-05-01 10:00:11 -04:00
// Verify for all manufacturers.
virtual uint16_t getSize();
2023-02-26 09:02:10 -05:00
// Returns size in BYTE
uint32_t getSizeBytes();
2023-05-01 10:00:11 -04:00
// override when getSize() fails == 0 (see readme.md)
2023-02-26 09:02:10 -05:00
void setSizeBytes(uint32_t value);
2022-05-02 10:06:10 -04:00
2023-09-07 08:07:49 -04:00
// SLEEP - 0.3.6
2022-05-02 10:06:10 -04:00
void sleep();
2023-09-17 04:13:35 -04:00
// timeRecover <= 400us see trec P12
bool wakeup(uint32_t timeRecover = 400);
2022-02-08 10:47:34 -05:00
2021-01-29 06:31:58 -05:00
2022-05-10 04:10:44 -04:00
protected:
2021-01-29 06:31:58 -05:00
uint8_t _address;
2022-03-25 05:04:48 -04:00
uint32_t _sizeBytes;
2022-02-08 10:47:34 -05:00
// default no pin = -1 ==> no write protect.
int8_t _writeProtectPin = -1;
2022-03-25 05:04:48 -04:00
TwoWire* _wire;
2021-01-29 06:31:58 -05:00
2023-09-07 08:07:49 -04:00
uint32_t _getMetaData();
2023-05-01 10:00:11 -04:00
// virtual so derived classes FRAM9/11 use their implementation.
2023-09-07 08:07:49 -04:00
virtual void _writeBlock(uint16_t memAddr, uint8_t * obj, uint8_t size);
virtual void _readBlock(uint16_t memAddr, uint8_t * obj, uint8_t size);
2021-01-29 06:31:58 -05:00
};
2021-12-18 09:02:32 -05:00
2023-02-26 09:02:10 -05:00
/////////////////////////////////////////////////////////////////////////////
2022-05-10 04:10:44 -04:00
//
// FRAM32
//
class FRAM32 : public FRAM
{
public:
FRAM32(TwoWire *wire = &Wire);
2023-09-07 08:07:49 -04:00
void write8(uint32_t memAddr, uint8_t value);
void write16(uint32_t memAddr, uint16_t value);
void write32(uint32_t memAddr, uint32_t value);
void write64(uint32_t memAddr, uint64_t value);
void writeFloat(uint32_t memAddr, float value);
void writeDouble(uint32_t memAddr, double value);
void write(uint32_t memAddr, uint8_t * obj, uint16_t size);
uint8_t read8(uint32_t memAddr);
uint16_t read16(uint32_t memAddr);
uint32_t read32(uint32_t memAddr);
uint64_t read64(uint32_t memAddr);
float readFloat(uint32_t memAddr);
double readDouble(uint32_t memAddr);
void read(uint32_t memAddr, uint8_t * obj, uint16_t size);
2022-05-10 04:10:44 -04:00
2023-04-22 05:34:55 -04:00
// readUntil returns length 0.. n of the buffer.
// readUntil returns -1 if data does not fit into buffer,
// => separator not encountered.
2023-09-07 08:07:49 -04:00
int32_t readUntil(uint32_t memAddr, char * buffer, uint16_t bufferLength, char separator);
2023-05-01 10:00:11 -04:00
// buffer needs one place for end char '\0'.
2023-09-07 08:07:49 -04:00
int32_t readLine(uint32_t memAddr, char * buffer, uint16_t bufferLength);
2023-05-01 10:00:11 -04:00
2023-09-17 04:13:35 -04:00
// 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);
};
2023-09-25 12:43:38 -04:00
2023-09-17 04:13:35 -04:00
template <class T> uint32_t readObject(uint32_t memAddr, T &obj)
{
read(memAddr, (uint8_t *) &obj, sizeof(obj));
return memAddr + sizeof(obj);
}
2022-05-10 04:10:44 -04:00
uint32_t clear(uint8_t value = 0); // fills FRAM with value
2023-09-07 08:07:49 -04:00
2022-05-10 04:10:44 -04:00
protected:
2023-09-07 08:07:49 -04:00
void _writeBlock(uint32_t memAddr, uint8_t * obj, uint8_t size);
void _readBlock(uint32_t memAddr, uint8_t * obj, uint8_t size);
2022-05-10 04:10:44 -04:00
};
2023-02-26 09:02:10 -05:00
/////////////////////////////////////////////////////////////////////////////
//
// FRAM11 for FRAM that use 11 bits addresses - e.g. MB85RC16
//
class FRAM11 : public FRAM
{
public:
FRAM11(TwoWire *wire = &Wire);
2023-05-01 10:00:11 -04:00
// address and writeProtectPin is optional
int begin(const uint8_t address = 0x50,
const int8_t writeProtectPin = -1);
2023-02-26 09:02:10 -05:00
uint16_t getSize();
2023-09-07 08:07:49 -04:00
2023-02-26 09:02:10 -05:00
protected:
2023-09-07 08:07:49 -04:00
void _writeBlock(uint16_t memAddr, uint8_t * obj, uint8_t size);
void _readBlock(uint16_t memAddr, uint8_t * obj, uint8_t size);
2023-02-26 09:02:10 -05:00
};
/////////////////////////////////////////////////////////////////////////////
//
// FRAM9 for FRAM that use 9 bits addresses - e.g. MB85RC04
//
class FRAM9 : public FRAM
{
public:
FRAM9(TwoWire *wire = &Wire);
2023-05-01 10:00:11 -04:00
// address and writeProtectPin is optional
int begin(const uint8_t address = 0x50,
const int8_t writeProtectPin = -1);
2023-02-26 09:02:10 -05:00
uint16_t getSize();
2023-09-07 08:07:49 -04:00
2023-02-26 09:02:10 -05:00
protected:
2023-09-07 08:07:49 -04:00
void _writeBlock(uint16_t memAddr, uint8_t * obj, uint8_t size);
void _readBlock(uint16_t memAddr, uint8_t * obj, uint8_t size);
2023-02-26 09:02:10 -05:00
};
// -- END OF FILE --
2021-12-18 09:02:32 -05:00