GY-63_MS5611/libraries/I2C_24LC1025/I2C_24LC1025.h

113 lines
3.8 KiB
C
Raw Normal View History

2021-02-03 11:20:20 -05:00
#pragma once
//
// FILE: I2C_24LC1025.h
// AUTHOR: Rob Tillaart
2022-11-12 09:41:55 -05:00
// VERSION: 0.2.2
2022-06-08 04:45:22 -04:00
// PURPOSE: I2C_24LC1025 library for Arduino with EEPROM 24LC1025 et al.
2021-02-03 11:20:20 -05:00
// URL: https://github.com/RobTillaart/I2C_24LC1025
2022-06-12 11:10:15 -04:00
//
2022-11-12 09:41:55 -05:00
// HISTORY: See changelog.md
2021-02-03 11:20:20 -05:00
#include "Arduino.h"
#include "Wire.h"
2022-11-12 09:41:55 -05:00
#define I2C_24LC1025_VERSION (F("0.2.2"))
2021-02-03 11:20:20 -05:00
2021-09-02 01:36:22 -04:00
#define I2C_DEVICESIZE_24LC1025 131072
#define I2C_PAGESIZE_24LC1025 128
2021-02-03 11:20:20 -05:00
class I2C_24LC1025
{
public:
I2C_24LC1025(uint8_t deviceAddress, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl);
#endif
bool begin();
bool isConnected();
2021-12-19 11:08:13 -05:00
// writes a byte to memoryAddress
2022-06-08 04:45:22 -04:00
// returns I2C status, 0 = OK
2021-02-03 11:20:20 -05:00
int writeByte(const uint32_t memoryAddress, const uint8_t value);
2021-12-19 11:08:13 -05:00
// writes length bytes from buffer to EEPROM
2022-06-08 04:45:22 -04:00
// returns I2C status, 0 = OK
2021-02-03 11:20:20 -05:00
int writeBlock(const uint32_t memoryAddress, const uint8_t *buffer, const uint32_t length);
2021-12-19 11:08:13 -05:00
// set length bytes in the EEPROM to the same value.
2022-06-08 04:45:22 -04:00
// returns I2C status, 0 = OK
2021-02-03 11:20:20 -05:00
int setBlock(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);
2021-12-19 11:08:13 -05:00
// returns the value stored in memoryAddress
2021-02-03 11:20:20 -05:00
uint8_t readByte(const uint32_t memoryAddress);
2021-12-19 11:08:13 -05:00
// reads length bytes into buffer
2022-06-08 04:45:22 -04:00
// returns bytes read.
2022-06-12 11:10:15 -04:00
uint32_t readBlock(const uint32_t memoryAddress, uint8_t * buffer, const uint32_t length);
2021-02-03 11:20:20 -05:00
2022-06-08 04:45:22 -04:00
2022-06-12 11:10:15 -04:00
// updates a byte at memoryAddress, writes only if there is a new value.
2021-12-19 11:08:13 -05:00
// return 0 if data is same or written OK, error code otherwise.
2021-02-03 11:20:20 -05:00
int updateByte(const uint32_t memoryAddress, const uint8_t value);
2021-12-19 11:08:13 -05:00
// updates a block in memory, writes only if there is a new value.
2022-06-12 11:10:15 -04:00
// only to be used when you expect to write same buffer multiple times.
2021-12-19 11:08:13 -05:00
// test your performance gains!
2022-06-08 04:45:22 -04:00
// returns bytes written.
uint32_t updateBlock(const uint32_t memoryAddress, const uint8_t* buffer, const uint32_t length);
// same functions as above but with verify
// return false if write or verify failed.
bool writeByteVerify(const uint32_t memoryAddress, const uint8_t value);
bool writeBlockVerify(const uint32_t memoryAddress, const uint8_t * buffer, const uint32_t length);
bool setBlockVerify(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);
bool updateByteVerify(const uint32_t memoryAddress, const uint8_t value);
bool updateBlockVerify(const uint32_t memoryAddress, const uint8_t * buffer, const uint32_t length);
2021-02-03 11:20:20 -05:00
2022-06-08 04:45:22 -04:00
// Meta data functions
2021-02-03 11:20:20 -05:00
uint32_t getDeviceSize() { return _deviceSize; };
uint8_t getPageSize() { return _pageSize; };
uint32_t getLastWrite() { return _lastWrite; };
2022-06-12 11:10:15 -04:00
// TWR = WriteCycleTime
// 5 ms is minimum, one can add extra ms here to adjust timing of both read() and write()
void setExtraWriteCycleTime(uint8_t ms) { _extraTWR = ms; };
uint8_t getExtraWriteCycleTime() { return _extraTWR; };
2021-02-03 11:20:20 -05:00
private:
uint8_t _deviceAddress;
uint8_t _actualAddress; // a.k.a. controlByte
2022-06-12 11:10:15 -04:00
uint32_t _lastWrite = 0; // for waitEEReady
2021-09-02 01:36:22 -04:00
uint32_t _deviceSize = I2C_DEVICESIZE_24LC1025;
uint8_t _pageSize = I2C_PAGESIZE_24LC1025;
2022-06-12 11:10:15 -04:00
uint8_t _extraTWR = 0; // milliseconds
int _error = 0; // TODO.
2021-02-03 11:20:20 -05:00
void _beginTransmission(uint32_t memoryAddress);
2022-06-08 04:45:22 -04:00
// returns I2C status, 0 = OK
2022-06-12 11:10:15 -04:00
int _pageBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint16_t length, const bool incrBuffer);
2022-06-08 04:45:22 -04:00
// returns I2C status, 0 = OK
2022-06-12 11:10:15 -04:00
int _WriteBlock(uint32_t memoryAddress, const uint8_t * buffer, const uint8_t length);
2022-06-08 04:45:22 -04:00
// returns bytes read.
2022-06-12 11:10:15 -04:00
uint8_t _ReadBlock(uint32_t memoryAddress, uint8_t * buffer, const uint8_t length);
// to optimize the write latency of the EEPROM
void _waitEEReady();
2021-02-03 11:20:20 -05:00
TwoWire * _wire;
2022-06-12 11:10:15 -04:00
bool _debug = false;
2021-02-03 11:20:20 -05:00
};
2021-12-19 11:08:13 -05:00
2021-02-03 11:20:20 -05:00
// -- END OF FILE --
2021-12-19 11:08:13 -05:00