2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: FRAM.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-16 13:09:32 -04:00
|
|
|
// VERSION: 0.4.2
|
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"
|
|
|
|
|
|
|
|
|
2022-10-16 13:09:32 -04:00
|
|
|
#define FRAM_LIB_VERSION (F("0.4.2"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#define FRAM_OK 0
|
|
|
|
#define FRAM_ERROR_ADDR -10
|
|
|
|
#define FRAM_ERROR_I2C -11
|
|
|
|
#define FRAM_ERROR_CONNECT -12
|
|
|
|
|
2022-03-25 05:04:48 -04:00
|
|
|
// Size known types
|
|
|
|
#define FRAM_MB85RC04 512
|
|
|
|
#define FRAM_MB85RC16 2048
|
|
|
|
#define FRAM_MB85RC64T 8192
|
|
|
|
#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);
|
|
|
|
|
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
|
|
|
// address and writeProtectPin is optional
|
2022-09-25 04:04:25 -04:00
|
|
|
int begin(int sda, int scl, const uint8_t address = 0x50,
|
|
|
|
const int8_t writeProtectPin = -1);
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
|
|
|
// address and writeProtectPin is optional
|
2022-09-25 04:04:25 -04:00
|
|
|
int begin(const uint8_t address = 0x50,
|
|
|
|
const int8_t writeProtectPin = -1);
|
2021-01-29 06:31:58 -05:00
|
|
|
bool isConnected();
|
|
|
|
|
|
|
|
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 write(uint16_t memaddr, uint8_t * obj, uint16_t size);
|
|
|
|
|
|
|
|
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 * obj, uint16_t size);
|
|
|
|
|
2022-03-18 08:37:57 -04:00
|
|
|
template <class T> uint16_t writeObject(uint16_t memaddr, T &obj)
|
|
|
|
{
|
|
|
|
write(memaddr, (uint8_t *) &obj, sizeof(obj));
|
|
|
|
return memaddr + sizeof(obj);
|
|
|
|
};
|
|
|
|
template <class T> uint16_t readObject(uint16_t memaddr, T &obj)
|
|
|
|
{
|
|
|
|
read(memaddr, (uint8_t *) &obj, sizeof(obj));
|
|
|
|
return memaddr + sizeof(obj);
|
|
|
|
}
|
|
|
|
|
2022-02-08 10:47:34 -05:00
|
|
|
// works only if pin is defined in begin.
|
2021-01-29 06:31:58 -05:00
|
|
|
bool setWriteProtect(bool b);
|
|
|
|
bool getWriteProtect();
|
|
|
|
|
2022-02-08 10:47:34 -05:00
|
|
|
// meta info
|
2022-03-25 05:04:48 -04:00
|
|
|
uint16_t getManufacturerID(); // Fujitsu = 0x000A
|
|
|
|
uint16_t getProductID(); // Proprietary
|
|
|
|
uint16_t getSize(); // Returns size in KILO-BYTE (or 0)
|
2022-10-16 13:09:32 -04:00
|
|
|
uint32_t getSizeBytes(); // Returns size in BYTE
|
|
|
|
void setSizeBytes(uint32_t value); // override when getSize() fails == 0
|
2022-03-25 05:04:48 -04:00
|
|
|
|
2022-05-02 10:06:10 -04:00
|
|
|
uint32_t clear(uint8_t value = 0); // fills FRAM with value
|
|
|
|
|
|
|
|
// 0.3.6
|
|
|
|
void sleep();
|
|
|
|
bool wakeup(uint32_t trec = 400); // trec <= 400us P12
|
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
|
|
|
|
2022-03-25 05:04:48 -04:00
|
|
|
uint16_t _getMetaData(uint8_t id);
|
|
|
|
void _writeBlock(uint16_t memaddr, uint8_t * obj, uint8_t size);
|
|
|
|
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
|
|
|
|
2022-05-10 04:10:44 -04:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// FRAM32
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
class FRAM32 : public FRAM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FRAM32(TwoWire *wire = &Wire);
|
|
|
|
|
|
|
|
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 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);
|
|
|
|
void read(uint32_t memaddr, uint8_t * obj, uint16_t size);
|
|
|
|
|
2022-10-16 13:09:32 -04:00
|
|
|
template <class T> uint32_t writeObject(uint32_t memaddr, T &obj);
|
|
|
|
template <class T> uint32_t readObject(uint32_t memaddr, T &obj);
|
2022-05-10 04:10:44 -04:00
|
|
|
|
|
|
|
uint32_t clear(uint8_t value = 0); // fills FRAM with value
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void _writeBlock(uint32_t memaddr, uint8_t * obj, uint8_t size);
|
|
|
|
void _readBlock(uint32_t memaddr, uint8_t * obj, uint8_t size);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-18 09:02:32 -05:00
|
|
|
|