Skip to content

Commit 4183c39

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 6ce5d1d commit 4183c39

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

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)