|
| 1 | +/** |
| 2 | + * @file i2c.h |
| 3 | + * |
| 4 | + */ |
| 5 | +/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +#ifndef I2C_H_ |
| 27 | +#define I2C_H_ |
| 28 | + |
| 29 | +#include <cstdint> |
| 30 | + |
| 31 | +#include "gd32_i2c.h" |
| 32 | +#include "timing.h" |
| 33 | + |
| 34 | +namespace i2c { |
| 35 | +inline constexpr uint32_t kNormalSpeed = gd32::kI2CNormalSpeed; |
| 36 | +inline constexpr uint32_t kFullSpeed = gd32::kI2CFullSpeed; |
| 37 | + |
| 38 | +struct ReturnCode { |
| 39 | + static constexpr uint8_t kOk = GD32_I2C_OK; |
| 40 | + static constexpr uint8_t kNok = GD32_I2C_NOK; |
| 41 | + static constexpr uint8_t kNack = GD32_I2C_NACK; |
| 42 | + static constexpr uint8_t kNokLa = GD32_I2C_NOK_LA; |
| 43 | + static constexpr uint8_t kNokTout = GD32_I2C_NOK_TOUT; |
| 44 | +}; |
| 45 | + |
| 46 | +inline void Begin() { |
| 47 | + Gd32I2cBegin(); |
| 48 | +} |
| 49 | + |
| 50 | +inline void SetBaudrate(uint32_t baudrate) { |
| 51 | + Gd32I2cSetBaudrate(baudrate); |
| 52 | +} |
| 53 | + |
| 54 | +inline void SetAddress(uint8_t address) { |
| 55 | + Gd32I2cSetAddress(address); |
| 56 | +} |
| 57 | + |
| 58 | +inline uint8_t Write(const char* buffer, uint32_t length) { |
| 59 | + return Gd32I2cWrite(buffer, length); |
| 60 | +} |
| 61 | + |
| 62 | +inline uint8_t Write(uint8_t address, const char* buffer, uint32_t length) { |
| 63 | + return Gd32I2cWrite(address, buffer, length); |
| 64 | +} |
| 65 | + |
| 66 | +inline void Write(uint8_t data) { |
| 67 | + const char kBuffer[] = {static_cast<char>(data)}; |
| 68 | + Write(kBuffer, 1); |
| 69 | +} |
| 70 | + |
| 71 | +inline uint8_t Read(char* buffer, uint32_t length) { |
| 72 | + return Gd32I2cRead(buffer, length); |
| 73 | +} |
| 74 | + |
| 75 | +inline uint8_t Read(uint8_t address, char* buffer, uint32_t length) { |
| 76 | + return Gd32I2cRead(address, buffer, length); |
| 77 | +} |
| 78 | + |
| 79 | +inline uint16_t Read16() { |
| 80 | + char buf[2] = {0}; |
| 81 | + Read(buf, 2); |
| 82 | + return static_cast<uint16_t>(static_cast<uint16_t>(buf[0]) << 8 | static_cast<uint16_t>(buf[1])); |
| 83 | +} |
| 84 | + |
| 85 | +inline bool IsConnected(uint8_t address, uint32_t baudrate = kNormalSpeed) { |
| 86 | + return Gd32I2cIsConnected(address, baudrate); |
| 87 | +} |
| 88 | + |
| 89 | +inline void WriteReg(uint8_t reg, uint8_t value) { |
| 90 | + Gd32I2cWriteReg(reg, value); |
| 91 | +} |
| 92 | + |
| 93 | +inline void WriteReg(uint8_t reg, uint16_t value) { |
| 94 | + Gd32I2cWriteReg(reg, value); |
| 95 | +} |
| 96 | + |
| 97 | +inline void WriteReg(uint8_t address, uint8_t reg, uint8_t value) { |
| 98 | + Gd32I2cWriteReg(address, reg, value); |
| 99 | +} |
| 100 | + |
| 101 | +inline void ReadReg(uint8_t reg, uint8_t& value) { |
| 102 | + Gd32I2cReadReg(reg, value); |
| 103 | +} |
| 104 | + |
| 105 | +inline void ReadReg(uint8_t address, uint8_t reg, uint8_t& value) { |
| 106 | + Gd32I2cReadReg(address, reg, value); |
| 107 | +} |
| 108 | + |
| 109 | +inline uint16_t ReadRegister16(uint8_t reg) { |
| 110 | + const char kBuffer[] = {static_cast<char>(reg)}; |
| 111 | + i2c::Write(&kBuffer[0], 1); |
| 112 | + return Read16(); |
| 113 | +} |
| 114 | + |
| 115 | +inline uint16_t ReadRegister16DelayUs(uint8_t reg, uint32_t delay_us) { |
| 116 | + const char kBuffer[] = {static_cast<char>(reg)}; |
| 117 | + i2c::Write(&kBuffer[0], 1); |
| 118 | + timing::DelayUs(delay_us); |
| 119 | + return Read16(); |
| 120 | +} |
| 121 | +} // namespace i2c |
| 122 | + |
| 123 | +#endif // I2C_H_ |
0 commit comments