|
26 | 26 | #ifndef I2C_H_ |
27 | 27 | #define I2C_H_ |
28 | 28 |
|
| 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 | + |
29 | 38 | #include <cstdint> |
30 | 39 |
|
31 | 40 | #include "gd32_i2c.h" |
|
34 | 43 | namespace i2c { |
35 | 44 | inline constexpr uint32_t kNormalSpeed = gd32::kI2CNormalSpeed; |
36 | 45 | inline constexpr uint32_t kFullSpeed = gd32::kI2CFullSpeed; |
37 | | - |
| 46 | + |
38 | 47 | struct ReturnCode { |
39 | 48 | static constexpr uint8_t kOk = GD32_I2C_OK; |
40 | 49 | static constexpr uint8_t kNok = GD32_I2C_NOK; |
@@ -120,4 +129,113 @@ inline uint16_t ReadRegister16DelayUs(uint8_t reg, uint32_t delay_us) { |
120 | 129 | } |
121 | 130 | } // namespace i2c |
122 | 131 |
|
| 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 | + |
123 | 241 | #endif // I2C_H_ |
0 commit comments