Skip to content

Commit 1f456a7

Browse files
committed
Add I2c class and optimization pragmas
Add a lightweight I2c wrapper in lib-gd32/include/i2c.h that manages address and baudrate and provides Setup(), Read/Write helpers, register read/write (including 16-bit and delayed reads) and AckRead. Wrap both i2c and spi headers with conditional GCC push_options/pop_options and optimize pragmas to allow O2/O3 builds via CONFIG_*_OPTIMIZE_O2/ O3 flags. Also apply a minor whitespace cleanup in i2c.h.
1 parent 3a439f4 commit 1f456a7

2 files changed

Lines changed: 132 additions & 1 deletion

File tree

lib-gd32/include/i2c.h

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
#ifndef I2C_H_
2727
#define I2C_H_
2828

29+
#if defined(CONFIG_I2C_OPTIMIZE_O2) || defined(CONFIG_I2C_OPTIMIZE_O3)
30+
#pragma GCC push_options
31+
#if defined(CONFIG_I2C_OPTIMIZE_O2)
32+
#pragma GCC optimize("O2")
33+
#else
34+
#pragma GCC optimize("O3")
35+
#endif
36+
#endif
37+
2938
#include <cstdint>
3039

3140
#include "gd32_i2c.h"
@@ -34,7 +43,7 @@
3443
namespace i2c {
3544
inline constexpr uint32_t kNormalSpeed = gd32::kI2CNormalSpeed;
3645
inline constexpr uint32_t kFullSpeed = gd32::kI2CFullSpeed;
37-
46+
3847
struct ReturnCode {
3948
static constexpr uint8_t kOk = GD32_I2C_OK;
4049
static constexpr uint8_t kNok = GD32_I2C_NOK;
@@ -120,4 +129,113 @@ inline uint16_t ReadRegister16DelayUs(uint8_t reg, uint32_t delay_us) {
120129
}
121130
} // namespace i2c
122131

132+
class I2c {
133+
public:
134+
explicit I2c(uint8_t address, uint32_t baud_rate = i2c::kFullSpeed) : address_(address), baudrate_(baud_rate) {}
135+
136+
uint8_t GetAddress() const { return address_; }
137+
uint32_t GetBaudrate() const { return baudrate_; }
138+
139+
bool IsConnected() { return Gd32I2cIsConnected(address_, baudrate_); }
140+
141+
void Write(uint8_t data, bool do_setup) {
142+
const char kBuffer[] = {static_cast<char>(data)};
143+
if (do_setup) Setup();
144+
Gd32I2cWrite(kBuffer, 1);
145+
}
146+
147+
void Write(const char* data, uint32_t length) {
148+
Setup();
149+
Gd32I2cWrite(data, length);
150+
}
151+
152+
void WriteRegister(uint8_t reg, uint8_t value, bool do_setup) {
153+
const char kBuffer[] = {static_cast<char>(reg), static_cast<char>(value)};
154+
155+
if (do_setup) Setup();
156+
Gd32I2cWrite(kBuffer, 2);
157+
}
158+
159+
void WriteRegister(uint8_t reg, uint16_t value) {
160+
const char kBuffer[] = {static_cast<char>(reg), static_cast<char>(value >> 8), static_cast<char>(value & 0xFF)};
161+
162+
Setup();
163+
Gd32I2cWrite(kBuffer, 3);
164+
}
165+
166+
uint8_t Read(bool do_setup) {
167+
char buf[1] = {0};
168+
169+
if (do_setup) Setup();
170+
Gd32I2cRead(buf, 1);
171+
172+
return static_cast<uint8_t>(buf[0]);
173+
}
174+
175+
uint8_t Read(char* buffer, uint32_t length, bool do_setup) {
176+
if (do_setup) Setup();
177+
return Gd32I2cRead(buffer, length);
178+
}
179+
180+
uint16_t Read16(bool do_setup) {
181+
char buffer[2] = {0};
182+
183+
if (do_setup) Setup();
184+
Gd32I2cRead(buffer, 2);
185+
186+
return static_cast<uint16_t>(static_cast<uint16_t>(buffer[0]) << 8 | static_cast<uint16_t>(buffer[1]));
187+
}
188+
189+
uint8_t ReadRegister(uint8_t reg, bool do_setup) {
190+
const char kBuffer[] = {static_cast<char>(reg)};
191+
192+
if (do_setup) Setup();
193+
Gd32I2cWrite(&kBuffer[0], 1);
194+
195+
return Read(false);
196+
}
197+
198+
uint16_t ReadRegister16(uint8_t reg, bool do_setup) {
199+
const char kBuf[] = {static_cast<char>(reg)};
200+
201+
if (do_setup) Setup();
202+
Gd32I2cWrite(&kBuf[0], 1);
203+
204+
return Read16(false);
205+
}
206+
207+
uint16_t ReadRegister16DelayUs(uint8_t reg, uint32_t delay_us) {
208+
char buffer[2] = {0};
209+
210+
buffer[0] = static_cast<char>(reg);
211+
212+
Setup();
213+
Gd32I2cWrite(&buffer[0], 1);
214+
215+
timing::DelayUs(delay_us);
216+
217+
Gd32I2cRead(buffer, 2);
218+
219+
return static_cast<uint16_t>(static_cast<uint16_t>(buffer[0]) << 8 | static_cast<uint16_t>(buffer[1]));
220+
}
221+
222+
bool AckRead() {
223+
char buf;
224+
return Gd32I2cRead(&buf, 1) == 0;
225+
}
226+
227+
void Setup() {
228+
Gd32I2cSetAddress(address_);
229+
Gd32I2cSetBaudrate(baudrate_);
230+
}
231+
232+
private:
233+
uint8_t address_;
234+
uint32_t baudrate_;
235+
};
236+
237+
#if defined(CONFIG_I2C_OPTIMIZE_O2) || defined(CONFIG_I2C_OPTIMIZE_O3)
238+
#pragma GCC pop_options
239+
#endif
240+
123241
#endif // I2C_H_

lib-gd32/include/spi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
#ifndef SPI_H_
2727
#define SPI_H_
2828

29+
#if defined(CONFIG_SPI_OPTIMIZE_O2) || defined(CONFIG_SPI_OPTIMIZE_O3)
30+
#pragma GCC push_options
31+
#if defined(CONFIG_SPIOPTIMIZE_O2)
32+
#pragma GCC optimize("O2")
33+
#else
34+
#pragma GCC optimize("O3")
35+
#endif
36+
#endif
37+
2938
#include <cstdint>
3039

3140
#include "gd32_spi.h"
@@ -116,4 +125,8 @@ class Spi {
116125
uint8_t mode_;
117126
};
118127

128+
#if defined(CONFIG_SPI_OPTIMIZE_O2) || defined(CONFIG_SPI_OPTIMIZE_O3)
129+
#pragma GCC pop_options
130+
#endif
131+
119132
#endif // SPI_H_

0 commit comments

Comments
 (0)