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

497 lines
9.5 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: FRAM.cpp
// 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
//
2022-03-18 08:37:57 -04:00
// HISTORY: see CHANGELOG.md
2021-01-29 06:31:58 -05:00
#include "FRAM.h"
2022-02-08 10:47:34 -05:00
// DENSITY CODES
#define FRAM_MB85RC64 0x03
#define FRAM_MB85RC256 0x05
#define FRAM_MB85RC512 0x06
#define FRAM_MB85RC1M 0x07
2022-05-02 10:06:10 -04:00
// used for metadata and sleep
const uint8_t FRAM_SLAVE_ID_ = 0x7C; // == 0xF8
const uint8_t FRAM_SLEEP_CMD = 0x86; //
2021-01-29 06:31:58 -05:00
2021-12-18 09:02:32 -05:00
2021-01-29 06:31:58 -05:00
/////////////////////////////////////////////////////
//
// PUBLIC
//
FRAM::FRAM(TwoWire *wire)
{
_wire = wire;
_address = 0x50;
_writeProtectPin = -1;
2022-03-25 05:04:48 -04:00
_sizeBytes = 0;
2021-01-29 06:31:58 -05:00
}
#if defined (ESP8266) || defined(ESP32)
2022-09-25 04:04:25 -04:00
int FRAM::begin(int sda, int scl, const uint8_t address,
const int8_t writeProtectPin)
2021-01-29 06:31:58 -05:00
{
2022-03-25 05:04:48 -04:00
if ((address < 0x50) || (address > 0x57)) return FRAM_ERROR_ADDR;
2021-01-29 06:31:58 -05:00
_wire = &Wire;
_address = address;
if ((sda < 255) && (scl < 255))
{
_wire->begin(sda, scl);
} else {
_wire->begin();
}
if (writeProtectPin > -1)
{
_writeProtectPin = writeProtectPin;
pinMode(_writeProtectPin, OUTPUT);
}
if (! isConnected()) return FRAM_ERROR_CONNECT;
2022-03-25 05:04:48 -04:00
getSize();
2021-01-29 06:31:58 -05:00
return FRAM_OK;
}
#endif
2022-09-25 04:04:25 -04:00
int FRAM::begin(const uint8_t address,
const int8_t writeProtectPin)
2021-01-29 06:31:58 -05:00
{
2022-03-25 05:04:48 -04:00
if ((address < 0x50) || (address > 0x57)) return FRAM_ERROR_ADDR;
2021-01-29 06:31:58 -05:00
_address = address;
_wire->begin();
if (writeProtectPin > -1)
{
_writeProtectPin = writeProtectPin;
pinMode(_writeProtectPin, OUTPUT);
}
if (! isConnected()) return FRAM_ERROR_CONNECT;
2022-03-25 05:04:48 -04:00
getSize();
2021-01-29 06:31:58 -05:00
return FRAM_OK;
}
bool FRAM::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}
void FRAM::write8(uint16_t memaddr, uint8_t value)
{
uint8_t val = value;
2022-03-25 05:04:48 -04:00
_writeBlock(memaddr, (uint8_t *)&val, 1);
2021-01-29 06:31:58 -05:00
}
void FRAM::write16(uint16_t memaddr, uint16_t value)
{
uint16_t val = value;
2022-03-25 05:04:48 -04:00
_writeBlock(memaddr, (uint8_t *)&val, 2);
2021-01-29 06:31:58 -05:00
}
void FRAM::write32(uint16_t memaddr, uint32_t value)
{
uint32_t val = value;
2022-03-25 05:04:48 -04:00
_writeBlock(memaddr, (uint8_t *)&val, 4);
2021-01-29 06:31:58 -05:00
}
void FRAM::write(uint16_t memaddr, uint8_t * obj, uint16_t size)
{
const int blocksize = 24;
uint8_t * p = obj;
while (size >= blocksize)
{
2022-03-25 05:04:48 -04:00
_writeBlock(memaddr, p, blocksize);
2021-01-29 06:31:58 -05:00
memaddr += blocksize;
p += blocksize;
size -= blocksize;
}
// remaining
if (size > 0)
{
2022-03-25 05:04:48 -04:00
_writeBlock(memaddr, p, size);
2021-01-29 06:31:58 -05:00
}
}
uint8_t FRAM::read8(uint16_t memaddr)
{
uint8_t val;
2022-03-25 05:04:48 -04:00
_readBlock(memaddr, (uint8_t *)&val, 1);
2021-01-29 06:31:58 -05:00
return val;
}
uint16_t FRAM::read16(uint16_t memaddr)
{
uint16_t val;
2022-03-25 05:04:48 -04:00
_readBlock(memaddr, (uint8_t *)&val, 2);
2021-01-29 06:31:58 -05:00
return val;
}
uint32_t FRAM::read32(uint16_t memaddr)
{
uint32_t val;
2022-03-25 05:04:48 -04:00
_readBlock(memaddr, (uint8_t *)&val, 4);
2021-01-29 06:31:58 -05:00
return val;
}
void FRAM::read(uint16_t memaddr, uint8_t * obj, uint16_t size)
{
const uint8_t blocksize = 24;
uint8_t * p = obj;
while (size >= blocksize)
{
2022-03-25 05:04:48 -04:00
_readBlock(memaddr, p, blocksize);
2021-01-29 06:31:58 -05:00
memaddr += blocksize;
p += blocksize;
size -= blocksize;
}
// remainder
if (size > 0)
{
2022-03-25 05:04:48 -04:00
_readBlock(memaddr, p, size);
2021-01-29 06:31:58 -05:00
}
}
bool FRAM::setWriteProtect(bool b)
{
if (_writeProtectPin < 0) return false;
digitalWrite(_writeProtectPin, b ? HIGH : LOW);
return true;
}
bool FRAM::getWriteProtect()
{
if (_writeProtectPin < 0) return false;
return (digitalRead(_writeProtectPin) == HIGH);
}
uint16_t FRAM::getManufacturerID()
{
2022-03-25 05:04:48 -04:00
return _getMetaData(0);
2021-01-29 06:31:58 -05:00
}
uint16_t FRAM::getProductID()
{
2022-03-25 05:04:48 -04:00
return _getMetaData(1);
2021-01-29 06:31:58 -05:00
}
2022-03-25 05:04:48 -04:00
// NOTE: returns the size in kiloBYTE
2021-01-29 06:31:58 -05:00
uint16_t FRAM::getSize()
{
2022-03-25 05:04:48 -04:00
uint16_t density = _getMetaData(2);
uint16_t size = 0;
if (density > 0) size = (1UL << density);
_sizeBytes = size * 1024UL;
return size;
}
2022-10-16 13:09:32 -04:00
uint32_t FRAM::getSizeBytes()
{
return _sizeBytes;
};
2022-03-25 05:04:48 -04:00
// override to be used when getSize() fails == 0
void FRAM::setSizeBytes(uint32_t value)
{
_sizeBytes = value;
}
uint32_t FRAM::clear(uint8_t value)
{
uint8_t buf[16];
for (uint8_t i = 0; i < 16; i++) buf[i] = value;
uint32_t start = 0;
uint32_t end = _sizeBytes;
for (uint32_t addr = start; addr < end; addr += 16)
{
_writeBlock(addr, buf, 16);
}
return end - start;
2021-01-29 06:31:58 -05:00
}
2022-02-08 10:47:34 -05:00
2022-05-02 10:06:10 -04:00
// EXPERIMENTAL - to be confirmed
// page 12 datasheet
// command = S 0xF8 A address A S 86 A P (A = Ack from slave )
void FRAM::sleep()
{
_wire->beginTransmission(FRAM_SLAVE_ID_); // S 0xF8
_wire->write(_address << 1); // address << 1
_wire->endTransmission(false); // no stoP
_wire->beginTransmission(FRAM_SLEEP_CMD >> 1); // S 0x86
_wire->endTransmission(true); // stoP
}
// page 12 datasheet trec <= 400us
bool FRAM::wakeup(uint32_t trec)
{
bool b = isConnected(); // wakeup
if (trec == 0) return b;
// wait recovery time
delayMicroseconds(trec);
return isConnected(); // check recovery OK
}
2021-01-29 06:31:58 -05:00
///////////////////////////////////////////////////////////
//
// PRIVATE
//
2022-02-08 10:47:34 -05:00
// metadata is packed as [....MMMM][MMMMDDDD][PPPPPPPP]
// M = manufacturerID
// D = density => memory size = 2^D KB
// P = product ID (together with D)
2022-03-25 05:04:48 -04:00
uint16_t FRAM::_getMetaData(uint8_t field)
2021-01-29 06:31:58 -05:00
{
if (field > 2) return 0;
_wire->beginTransmission(FRAM_SLAVE_ID_);
_wire->write(_address << 1);
_wire->endTransmission(false);
int x = _wire->requestFrom(FRAM_SLAVE_ID_, (uint8_t)3);
if (x != 3) return -1;
uint32_t value = 0;
value = _wire->read();
value = value << 8;
value |= _wire->read();
value = value << 8;
value |= _wire->read();
// MANUFACTURER
if (field == 0) return (value >> 12) & 0xFF;
// PRODUCT ID
if (field == 1) return value & 0x0FFF;
// DENSITY
2022-02-08 10:47:34 -05:00
// 3 => MB85RC64
// 5 => MB85RC256
// 6 => MB85RC512
// 7 => MB85RC1M
2021-01-29 06:31:58 -05:00
if (field == 2) return (value >> 8) & 0x0F;
return 0;
}
2022-03-25 05:04:48 -04:00
void FRAM::_writeBlock(uint16_t memaddr, uint8_t * obj, uint8_t size)
2021-01-29 06:31:58 -05:00
{
_wire->beginTransmission(_address);
2022-05-10 04:10:44 -04:00
_wire->write((uint8_t) (memaddr >> 8));
_wire->write((uint8_t) (memaddr & 0xFF));
2021-01-29 06:31:58 -05:00
uint8_t * p = obj;
2022-05-10 04:10:44 -04:00
for (uint8_t i = size; i > 0; i--)
2021-01-29 06:31:58 -05:00
{
_wire->write(*p++);
}
_wire->endTransmission();
}
2022-03-25 05:04:48 -04:00
void FRAM::_readBlock(uint16_t memaddr, uint8_t * obj, uint8_t size)
2021-01-29 06:31:58 -05:00
{
_wire->beginTransmission(_address);
2022-05-10 04:10:44 -04:00
_wire->write((uint8_t) (memaddr >> 8));
_wire->write((uint8_t) (memaddr & 0xFF));
2021-01-29 06:31:58 -05:00
_wire->endTransmission();
_wire->requestFrom(_address, size);
uint8_t * p = obj;
2022-05-10 04:10:44 -04:00
for (uint8_t i = size; i > 0; i--)
{
*p++ = _wire->read();
}
}
/////////////////////////////////////////////////////////////////
//
// FRAM32 PUBLIC
//
FRAM32::FRAM32(TwoWire *wire):FRAM(wire)
{
}
void FRAM32::write8(uint32_t memaddr, uint8_t value)
{
uint8_t val = value;
_writeBlock(memaddr, (uint8_t *)&val, 1);
}
void FRAM32::write16(uint32_t memaddr, uint16_t value)
{
uint16_t val = value;
_writeBlock(memaddr, (uint8_t *)&val, 2);
}
void FRAM32::write32(uint32_t memaddr, uint32_t value)
{
uint32_t val = value;
_writeBlock(memaddr, (uint8_t *)&val, 4);
}
void FRAM32::write(uint32_t memaddr, uint8_t * obj, uint16_t size)
{
const int blocksize = 24;
uint8_t * p = obj;
while (size >= blocksize)
{
_writeBlock(memaddr, p, blocksize);
memaddr += blocksize;
p += blocksize;
size -= blocksize;
}
// remaining
if (size > 0)
{
_writeBlock(memaddr, p, size);
}
}
uint8_t FRAM32::read8(uint32_t memaddr)
{
uint8_t val;
_readBlock(memaddr, (uint8_t *)&val, 1);
return val;
}
uint16_t FRAM32::read16(uint32_t memaddr)
{
uint16_t val;
_readBlock(memaddr, (uint8_t *)&val, 2);
return val;
}
uint32_t FRAM32::read32(uint32_t memaddr)
{
uint32_t val;
_readBlock(memaddr, (uint8_t *)&val, 4);
return val;
}
void FRAM32::read(uint32_t memaddr, uint8_t * obj, uint16_t size)
{
const uint8_t blocksize = 24;
uint8_t * p = obj;
while (size >= blocksize)
{
_readBlock(memaddr, p, blocksize);
memaddr += blocksize;
p += blocksize;
size -= blocksize;
}
// remainder
if (size > 0)
{
_readBlock(memaddr, p, size);
}
}
2022-10-16 13:09:32 -04:00
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);
}
2022-05-10 04:10:44 -04:00
uint32_t FRAM32::clear(uint8_t value)
{
uint8_t buf[16];
for (uint8_t i = 0; i < 16; i++) buf[i] = value;
uint32_t start = 0;
uint32_t end = _sizeBytes;
for (uint32_t addr = start; addr < end; addr += 16)
{
_writeBlock(addr, buf, 16);
}
return end - start;
}
///////////////////////////////////////////////////////////
//
// FRAM32 PROTECTED
//
void FRAM32::_writeBlock(uint32_t memaddr, uint8_t * obj, uint8_t size)
{
uint8_t _addr = _address;
if (memaddr & 0x00010000) _addr += 0x01;
_wire->beginTransmission(_addr);
_wire->write((uint8_t) (memaddr >> 8));
_wire->write((uint8_t) (memaddr & 0xFF));
uint8_t * p = obj;
for (uint8_t i = size; i > 0; i--)
{
_wire->write(*p++);
}
_wire->endTransmission();
}
void FRAM32::_readBlock(uint32_t memaddr, uint8_t * obj, uint8_t size)
{
uint8_t _addr = _address;
if (memaddr & 0x00010000) _addr += 0x01;
_wire->beginTransmission(_address);
_wire->write((uint8_t) (memaddr >> 8));
_wire->write((uint8_t) (memaddr & 0xFF));
_wire->endTransmission();
_wire->requestFrom(_addr, size);
uint8_t * p = obj;
for (uint8_t i = size; i > 0; i--)
2021-01-29 06:31:58 -05:00
{
*p++ = _wire->read();
}
}
2022-02-08 10:47:34 -05:00
2021-01-29 06:31:58 -05:00
// -- END OF FILE --
2022-02-08 10:47:34 -05:00