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

134 lines
4.1 KiB
C
Raw Normal View History

2021-02-03 11:20:20 -05:00
#pragma once
//
// FILE: I2C_24LC1025.h
// AUTHOR: Rob Tillaart
2023-11-24 09:08:56 -05:00
// VERSION: 0.3.0
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
#include "Arduino.h"
#include "Wire.h"
2023-11-24 09:08:56 -05:00
#define I2C_24LC1025_LIB_VERSION (F("0.3.0"))
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
2023-05-02 07:37:13 -04:00
// to adjust low level timing (use with care)
// can also be done on command line.
2023-09-11 04:03:32 -04:00
// (see private _waitEEReady() function)
2023-05-02 07:37:13 -04:00
#ifndef I2C_WRITEDELAY
#define I2C_WRITEDELAY 5000
#endif
2021-02-03 11:20:20 -05:00
class I2C_24LC1025
{
public:
I2C_24LC1025(uint8_t deviceAddress, TwoWire *wire = &Wire);
2023-05-11 06:39:35 -04:00
2023-09-11 04:03:32 -04:00
// use default I2C pins.
bool begin(int8_t writeProtectPin = -1);
bool isConnected();
2023-11-24 09:08:56 -05:00
uint8_t getAddress();
2021-02-03 11:20:20 -05:00
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
2023-09-11 04:03:32 -04: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
2023-09-11 04:03:32 -04: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
2023-09-11 04:03:32 -04:00
int setBlock(const uint32_t memoryAddress, const uint8_t value, const uint32_t length);
2021-02-03 11:20:20 -05:00
2021-12-19 11:08:13 -05:00
// returns the value stored in memoryAddress
2023-09-11 04:03:32 -04: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.
2023-09-11 04:03:32 -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
2023-05-02 07:37:13 -04:00
uint32_t getDeviceSize();
uint8_t getPageSize();
uint32_t getLastWrite();
2021-02-03 11:20:20 -05:00
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()
2023-05-02 07:37:13 -04:00
void setExtraWriteCycleTime(uint8_t ms);
uint8_t getExtraWriteCycleTime();
2021-02-03 11:20:20 -05:00
2023-09-11 04:03:32 -04:00
// WRITEPROTECT
// works only if WP pin is defined in begin().
// see readme.md
inline bool hasWriteProtectPin();
void allowWrite();
void preventWrite();
void setAutoWriteProtect(bool b);
bool getAutoWriteProtect();
2021-02-03 11:20:20 -05:00
private:
uint8_t _deviceAddress;
2023-05-02 07:37:13 -04:00
uint8_t _actualAddress; // a.k.a. controlByte
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;
2023-05-02 07:37:13 -04:00
uint8_t _extraTWR = 0; // milliseconds
int _error = 0; // TODO.
2021-02-03 11:20:20 -05:00
2023-09-11 04:03:32 -04:00
void _beginTransmission(uint32_t memoryAddress);
2021-02-03 11:20:20 -05:00
2022-06-08 04:45:22 -04:00
// returns I2C status, 0 = OK
2023-09-11 04:03:32 -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
2023-09-11 04:03:32 -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;
2023-09-11 04:03:32 -04:00
bool _debug = false;
int8_t _writeProtectPin = -1;
bool _autoWriteProtect = false;
2021-02-03 11:20:20 -05:00
};
2021-12-19 11:08:13 -05:00
2023-05-02 07:37:13 -04:00
// -- END OF FILE --
2021-12-19 11:08:13 -05:00