-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathI2CEEPROM.h
94 lines (82 loc) · 3.51 KB
/
I2CEEPROM.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* \brief I2C EEPROMs data writer/reader library
*
* \author Quentin Comte-Gaz <[email protected]>
* \date 15 January 2023
* \license MIT License (contact me if too restrictive)
* \copyright Copyright (c) 2023 Quentin Comte-Gaz
* \version 1.3
*
* \history
* - v1.0 Main design of the library
* - v1.1 Add Arduino < 1.0 compatibility
* - v1.2 Add CAT24Cxx compatibility
* - v1.3 Add update() method
*/
#ifndef I2CEEPROM_h
#define I2CEEPROM_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Internal enum to set the number of bits used in I2C address for the memory address for CAT24CXX devices.
// The "Classic" device sends the address and then 16-bits for the address.
// The CAT24CXX devices include the 'n' MSB of the MEMORY address as the 'n' LSB of the I2C address.
// Where n is 3 for the 16 kb, 2 for the 8 kb, 1 for the 4 kb, and 0 for the 2kb
enum address_mode
{
ADDRESS_MODE_16BIT,
ADDRESS_MODE_8BIT = 0,
ADDRESS_MODE_9BIT = 1,
ADDRESS_MODE_10BIT = 2,
ADDRESS_MODE_11BIT = 3
};
// Map device names to Addressing mode to make it easier to use
#define EEPROM_DEVICE_CLASSIC ADDRESS_MODE_16BIT
#define EEPROM_DEVICE_CAT24C01 ADDRESS_MODE_8BIT
#define EEPROM_DEVICE_CAT24C02 ADDRESS_MODE_8BIT
#define EEPROM_DEVICE_CAT24C04 ADDRESS_MODE_9BIT
#define EEPROM_DEVICE_CAT24C08 ADDRESS_MODE_10BIT
#define EEPROM_DEVICE_CAT24C16 ADDRESS_MODE_11BIT
class I2CEEPROM
{
public:
/*!
* \brief I2CEEPROM Initialize I2C EEPROM instance
* \param device_address (int) I2C address of the EEPROM device
* \param addressing_mode (enum) Selects the device in use (defaults to "Classic" if not CAT24CXX)
* \param initialize_wire (bool) Initializes the Wire library (defaults to true)
*/
I2CEEPROM(int i2c_device_address = 0x50, enum address_mode addressing_mode = EEPROM_DEVICE_CLASSIC, bool initialize_wire = true);
/*!
* \brief write Write one byte \p data in EEPROM device at EEPROM internal address \p address
* \param address (unsigned int) EEPROM internal address (most of the time, first address is 0x00)
* \param data (byte) Byte to write at EEPROM internal address \p address
*/
void write(unsigned int address, byte data) const;
/*!
* \brief read Read one byte in EEPROM device at EEPROM internal address \p address
* \param address (unsigned int) EEPROM internal address (most of the time, first address is 0x00)
* \return (byte) Read Byte at EEPROM internal address \p address (returns 0xFF if an error occurred)
*/
byte read(unsigned int address) const;
/*!
* \brief update Update one byte \p data in EEPROM device at EEPROM internal address \p address if not already the correct one (will read then write if not identical)
* \param address (unsigned int) EEPROM internal address (most of the time, first address is 0x00)
* \param data (byte) Byte to write at EEPROM internal address \p address
*/
void update(unsigned int address, byte data) const;
private:
/*!
* \brief read Combine I2C address with high byte of address for 512-2048 byte devices, return regular address for 16-bit addressed devices.
* \param address (uint16_t) EEPROM internal address
* \return (uint8_t) Read Byte at EEPROM internal address \p address (returns 0xFF if an error occurred)
*/
uint8_t generate_I2C_address(uint16_t address) const;
private:
int _i2c_device_address;
int _addressing_mode;
int _i2c_address_mask;
};
#endif //I2CEEPROM_h