22 * @file at24cxx.h
33 * @brief I2C interface for AT24Cxx EEPROM devices.
44 *
5- * This header defines a templated class for interfacing with
6- * AT24Cxx EEPROM devices over I2C, supporting multiple EEPROM types.
75 */
86/* Copyright (C) 2022-2026 by Arjan van Vught mailto:info@gd32-dmx.org
97 *
3634#include " i2c.h"
3735#include " common/utils/utils_math.h"
3836
39- /* *
40- * @namespace at24cxx
41- * @brief Contains constants and types for AT24Cxx EEPROM devices.
42- */
4337namespace at24cxx {
4438static constexpr uint8_t kI2CAddress = 0x50 ;
4539
46- /* *
47- * @struct ATTypes
48- * @brief Defines the sizes of different AT24Cxx EEPROM devices in bytes.
49- */
5040struct ATTypes {
5141 static constexpr uint32_t kAT24LC512 = 65536 ;
5242 static constexpr uint32_t kAT24LC256 = 32768 ;
@@ -61,41 +51,31 @@ struct ATTypes {
6151};
6252} // namespace at24cxx
6353
64- /* *
65- * @class AT24Cxx
66- * @brief Template class for interfacing with AT24Cxx EEPROM devices.
67- *
68- * This class provides methods to read and write to AT24Cxx devices
69- * using I2C communication. The `type` parameter defines the EEPROM
70- * type and size.
71- *
72- * @tparam type Size of the EEPROM in bytes.
73- */
7454template <uint32_t kType >
7555class AT24Cxx {
7656 static constexpr bool IsValidType () {
77- return kType == at24cxx::ATTypes::kAT24LC512 || kType == at24cxx::ATTypes::kAT24LC256 || kType == at24cxx::ATTypes::kAT24LC128 || kType == at24cxx::ATTypes::kAT24LC64 || kType == at24cxx::ATTypes::kAT24LC32 ||
78- kType == at24cxx::ATTypes::kAT24LC16 || kType == at24cxx::ATTypes::kAT24LC08 || kType == at24cxx::ATTypes::kAT24LC04 || kType == at24cxx::ATTypes::kAT24LC02 || kType == at24cxx::ATTypes::kAT24LC01 ;
57+ return kType == at24cxx::ATTypes::kAT24LC512 ||
58+ kType == at24cxx::ATTypes::kAT24LC256 ||
59+ kType == at24cxx::ATTypes::kAT24LC128 ||
60+ kType == at24cxx::ATTypes::kAT24LC64 ||
61+ kType == at24cxx::ATTypes::kAT24LC32 ||
62+ kType == at24cxx::ATTypes::kAT24LC16 ||
63+ kType == at24cxx::ATTypes::kAT24LC08 ||
64+ kType == at24cxx::ATTypes::kAT24LC04 ||
65+ kType == at24cxx::ATTypes::kAT24LC02 ||
66+ kType == at24cxx::ATTypes::kAT24LC01 ;
7967 }
8068
8169 public:
82- /* *
83- * @brief Constructor for AT24Cxx.
84- *
85- * @param device_address I2C slave address of the EEPROM device.
86- */
8770 explicit AT24Cxx (uint8_t device_address) : address_(device_address) {
8871 static_assert (IsValidType (), " Invalid type specified for AT24Cxx." );
8972 connected_ = i2c::IsConnected (address_, i2c::kFullSpeed );
9073 }
9174
92- /* * @brief Checks if the EEPROM device is connected. */
9375 [[nodiscard]] bool IsConnected () const { return connected_; }
9476
95- /* * @brief Returns the I2C address of the EEPROM device. */
9677 [[nodiscard]] uint8_t GetAddress () const { return address_; }
9778
98- /* * @brief Returns the size of the EEPROM device. */
9979 constexpr uint32_t GetSize () { return kType ; }
10080
10181 constexpr uint32_t GetPageSize () {
@@ -121,7 +101,8 @@ class AT24Cxx {
121101
122102 i2c::SetAddress (address_);
123103
124- while (!AckRead ());
104+ while (!AckRead ()) {
105+ }
125106
126107 if constexpr (kIsAddressSizeTwoWords ) {
127108 const char kBuffer [] = {static_cast <char >(memory_address >> 8 ), static_cast <char >(memory_address & 0xFF ), static_cast <char >(data)};
@@ -143,7 +124,8 @@ class AT24Cxx {
143124 i2c::SetAddress (address_);
144125
145126 while (!data.empty ()) {
146- while (!AckRead ());
127+ while (!AckRead ()) {
128+ }
147129
148130 const auto kOffsetPage = memory_address % GetPageSize ();
149131 uint32_t count;
@@ -189,9 +171,9 @@ class AT24Cxx {
189171 i2c::Write (kBuffer , sizeof (kBuffer ) / sizeof (kBuffer [0 ]));
190172 }
191173
192- char c ;
193- i2c::Read (&c , 1 );
194- return static_cast <uint8_t >(c );
174+ char character ;
175+ i2c::Read (&character , 1 );
176+ return static_cast <uint8_t >(character );
195177 }
196178
197179 uint8_t Read (uint32_t memory_address, std::span<uint8_t > data) {
@@ -201,7 +183,8 @@ class AT24Cxx {
201183
202184 i2c::SetAddress (address_);
203185
204- while (!AckRead ());
186+ while (!AckRead ()) {
187+ }
205188
206189 if constexpr (kIsAddressSizeTwoWords ) {
207190 const char kBuffer [] = {static_cast <char >(memory_address >> 8 ), static_cast <char >(memory_address & 0xFF )};
@@ -218,39 +201,35 @@ class AT24Cxx {
218201 }
219202
220203 private:
221- /* * @brief Performs an ACK read operation. */
222204 bool AckRead () {
223- char c ;
224- return i2c::Read (&c , 1 ) == 0 ;
205+ char character ;
206+ return i2c::Read (&character , 1 ) == 0 ;
225207 }
226208
227- /* * @brief Determines if the memory address size is 2 bytes. */
209+ // Determines if the memory address size is 2 bytes.
228210 static constexpr bool kIsAddressSizeTwoWords = kType > at24cxx::ATTypes::kAT24LC16 ;
229211
230212 uint8_t address_;
231213 bool connected_{false };
232214};
233215
234- /* *
235- * @class AT24C04
236- * @brief Specialized class for the AT24C04 EEPROM.
237- */
216+ class AT24C02 : public AT24Cxx <at24cxx::ATTypes::kAT24LC02 > {
217+ public:
218+ AT24C02 () : AT24Cxx(at24cxx::kI2CAddress ) {}
219+ };
220+
238221class AT24C04 : public AT24Cxx <at24cxx::ATTypes::kAT24LC04 > {
239222 public:
240223 AT24C04 () : AT24Cxx(at24cxx::kI2CAddress ) {}
241224};
242225
243- /* *
244- * @class AT24C32
245- * @brief Specialized class for the AT24C32 EEPROM.
246- */
226+ class AT24C16 : public AT24Cxx <at24cxx::ATTypes::kAT24LC16 > {
227+ public:
228+ AT24C16 () : AT24Cxx(at24cxx::kI2CAddress ) {}
229+ };
230+
247231class AT24C32 : public AT24Cxx <at24cxx::ATTypes::kAT24LC32 > {
248232 public:
249- /* *
250- * @brief Constructor for AT24C32.
251- *
252- * @param nIndex Index to select the appropriate I2C address.
253- */
254233 explicit AT24C32 (uint8_t index) : AT24Cxx(at24cxx::kI2CAddress + (index & 0x7 )) {}
255234};
256235
0 commit comments