Skip to content

Commit c1b7d14

Browse files
committed
Refine AT24Cxx and I2c const correctness
Cleans up the AT24Cxx EEPROM header by simplifying formatting, removing redundant comments, and improving readability in polling loops and local naming. Adds dedicated `AT24C02` and `AT24C16` convenience classes alongside existing specializations. Updates `I2c` with `[[nodiscard]]` getters and const-correct read/setup methods, and applies small consistency fixes in related headers/sources (debug include gating and Winbond table formatting).
1 parent c8ce439 commit c1b7d14

4 files changed

Lines changed: 61 additions & 68 deletions

File tree

lib-flash/include/i2c/at24cxx.h

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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
*
@@ -36,17 +34,9 @@
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-
*/
4337
namespace at24cxx {
4438
static constexpr uint8_t kI2CAddress = 0x50;
4539

46-
/**
47-
* @struct ATTypes
48-
* @brief Defines the sizes of different AT24Cxx EEPROM devices in bytes.
49-
*/
5040
struct 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-
*/
7454
template <uint32_t kType>
7555
class 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+
238221
class 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+
247231
class 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

lib-flash/src/spi/winbond.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ constexpr struct WinbondSpiFlashParams kWinbondSpiFlashTable[] = {
105105
.kId = 0x6015,
106106
.kNrBlocks = 32,
107107
.kName = "W25Q16DW",
108-
}};
108+
},};
109109
} // namespace
110110

111111
namespace spi::flash {

lib-flashcode/include/flashcode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
#include <cstdint>
3030
#include <span>
3131

32-
#include "firmware/debug/debug_debug.h"
33-
3432
#ifdef DEBUG_FLASHCODE
33+
#include "firmware/debug/debug_debug.h"
34+
3535
#define FLASHCODE_DEBUG_ENTRY() DEBUG_ENTRY()
3636
#define FLASHCODE_DEBUG_EXIT() DEBUG_EXIT()
3737
#define FLASHCODE_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)

lib-gd32/include/i2c.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ class I2c {
114114
public:
115115
explicit I2c(uint8_t address, uint32_t baud_rate = i2c::kFullSpeed) : address_(address), baudrate_(baud_rate) {}
116116

117-
uint8_t GetAddress() const { return address_; }
118-
uint32_t GetBaudrate() const { return baudrate_; }
117+
[[nodiscard]] uint8_t GetAddress() const { return address_; }
118+
[[nodiscard]] uint32_t GetBaudrate() const { return baudrate_; }
119119

120120
bool IsConnected() { return Gd32I2cIsConnected(address_, baudrate_); }
121121

@@ -133,35 +133,45 @@ class I2c {
133133
void WriteRegister(uint8_t reg, uint8_t value, bool do_setup) {
134134
const char kBuffer[] = {static_cast<char>(reg), static_cast<char>(value)};
135135

136-
if (do_setup) Setup();
136+
if (do_setup) {
137+
Setup();
138+
}
137139
Gd32I2cWrite(kBuffer, 2);
138140
}
139141

140142
void WriteRegister(uint8_t reg, uint16_t value, bool do_setup) {
141143
const char kBuffer[] = {static_cast<char>(reg), static_cast<char>(value >> 8), static_cast<char>(value & 0xFF)};
142144

143-
if (do_setup) Setup();
145+
if (do_setup) {
146+
Setup();
147+
}
144148
Gd32I2cWrite(kBuffer, 3);
145149
}
146150

147-
uint8_t Read(bool do_setup) {
151+
uint8_t Read(bool do_setup) const {
148152
char buf[1] = {0};
149153

150-
if (do_setup) Setup();
154+
if (do_setup) {
155+
Setup();
156+
}
151157
Gd32I2cRead(buf, 1);
152158

153159
return static_cast<uint8_t>(buf[0]);
154160
}
155161

156-
uint8_t Read(char* buffer, uint32_t length, bool do_setup) {
157-
if (do_setup) Setup();
162+
uint8_t Read(char* buffer, uint32_t length, bool do_setup) const {
163+
if (do_setup) {
164+
Setup();
165+
}
158166
return Gd32I2cRead(buffer, length);
159167
}
160168

161169
uint16_t Read16(bool do_setup) {
162170
char buffer[2] = {0};
163171

164-
if (do_setup) Setup();
172+
if (do_setup) {
173+
Setup();
174+
}
165175
Gd32I2cRead(buffer, 2);
166176

167177
return static_cast<uint16_t>(static_cast<uint16_t>(buffer[0]) << 8 | static_cast<uint16_t>(buffer[1]));
@@ -170,7 +180,9 @@ class I2c {
170180
uint8_t ReadRegister(uint8_t reg, bool do_setup) {
171181
const char kBuffer[] = {static_cast<char>(reg)};
172182

173-
if (do_setup) Setup();
183+
if (do_setup) {
184+
Setup();
185+
}
174186
Gd32I2cWrite(&kBuffer[0], 1);
175187

176188
return Read(false);
@@ -179,7 +191,9 @@ class I2c {
179191
uint16_t ReadRegister16(uint8_t reg, bool do_setup) {
180192
const char kBuf[] = {static_cast<char>(reg)};
181193

182-
if (do_setup) Setup();
194+
if (do_setup) {
195+
Setup();
196+
}
183197
Gd32I2cWrite(&kBuf[0], 1);
184198

185199
return Read16(false);
@@ -205,7 +219,7 @@ class I2c {
205219
return Gd32I2cRead(&buf, 1) == 0;
206220
}
207221

208-
void Setup() {
222+
void Setup() const {
209223
Gd32I2cSetAddress(address_);
210224
Gd32I2cSetBaudrate(baudrate_);
211225
}

0 commit comments

Comments
 (0)