Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 1 | #include "i2c.hpp" |
| 2 | |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 3 | #include <fcntl.h> |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 4 | #include <sys/ioctl.h> |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 5 | #include <sys/stat.h> |
| 6 | #include <unistd.h> |
| 7 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 8 | #include <cassert> |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 9 | #include <cerrno> |
| 10 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 11 | extern "C" |
| 12 | { |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 13 | #include <i2c/smbus.h> |
| 14 | #include <linux/i2c-dev.h> |
| 15 | #include <linux/i2c.h> |
| 16 | } |
| 17 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 18 | namespace i2c |
| 19 | { |
| 20 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 21 | unsigned long I2CDevice::getFuncs() |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 22 | { |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 23 | // If functionality has not been cached |
| 24 | if (cachedFuncs == NO_FUNCS) |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 25 | { |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 26 | // Get functionality from adapter |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 27 | int ret = 0, retries = 0; |
| 28 | do |
| 29 | { |
| 30 | ret = ioctl(fd, I2C_FUNCS, &cachedFuncs); |
| 31 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 32 | |
| 33 | if (ret < 0) |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 34 | { |
| 35 | throw I2CException("Failed to get funcs", busStr, devAddr, errno); |
| 36 | } |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 37 | } |
| 38 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 39 | return cachedFuncs; |
| 40 | } |
| 41 | |
| 42 | void I2CDevice::checkReadFuncs(int type) |
| 43 | { |
| 44 | unsigned long funcs = getFuncs(); |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 45 | switch (type) |
| 46 | { |
| 47 | case I2C_SMBUS_BYTE: |
| 48 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) |
| 49 | { |
| 50 | throw I2CException("Missing SMBUS_READ_BYTE", busStr, devAddr); |
| 51 | } |
| 52 | break; |
| 53 | case I2C_SMBUS_BYTE_DATA: |
| 54 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) |
| 55 | { |
| 56 | throw I2CException("Missing SMBUS_READ_BYTE_DATA", busStr, |
| 57 | devAddr); |
| 58 | } |
| 59 | break; |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 60 | case I2C_SMBUS_WORD_DATA: |
| 61 | if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) |
| 62 | { |
| 63 | throw I2CException("Missing SMBUS_READ_WORD_DATA", busStr, |
| 64 | devAddr); |
| 65 | } |
| 66 | break; |
| 67 | case I2C_SMBUS_BLOCK_DATA: |
| 68 | if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)) |
| 69 | { |
| 70 | throw I2CException("Missing SMBUS_READ_BLOCK_DATA", busStr, |
| 71 | devAddr); |
| 72 | } |
| 73 | break; |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 74 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 75 | if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 76 | { |
| 77 | throw I2CException("Missing I2C_FUNC_SMBUS_READ_I2C_BLOCK", |
| 78 | busStr, devAddr); |
| 79 | } |
| 80 | break; |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 81 | default: |
| 82 | fprintf(stderr, "Unexpected read size type: %d\n", type); |
| 83 | assert(false); |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 88 | void I2CDevice::checkWriteFuncs(int type) |
| 89 | { |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 90 | unsigned long funcs = getFuncs(); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 91 | switch (type) |
| 92 | { |
| 93 | case I2C_SMBUS_BYTE: |
| 94 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) |
| 95 | { |
| 96 | throw I2CException("Missing SMBUS_WRITE_BYTE", busStr, devAddr); |
| 97 | } |
| 98 | break; |
| 99 | case I2C_SMBUS_BYTE_DATA: |
| 100 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) |
| 101 | { |
| 102 | throw I2CException("Missing SMBUS_WRITE_BYTE_DATA", busStr, |
| 103 | devAddr); |
| 104 | } |
| 105 | break; |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 106 | case I2C_SMBUS_WORD_DATA: |
| 107 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) |
| 108 | { |
| 109 | throw I2CException("Missing SMBUS_WRITE_WORD_DATA", busStr, |
| 110 | devAddr); |
| 111 | } |
| 112 | break; |
| 113 | case I2C_SMBUS_BLOCK_DATA: |
| 114 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)) |
| 115 | { |
| 116 | throw I2CException("Missing SMBUS_WRITE_BLOCK_DATA", busStr, |
| 117 | devAddr); |
| 118 | } |
| 119 | break; |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 120 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 121 | if (!(funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) |
| 122 | { |
| 123 | throw I2CException("Missing I2C_FUNC_SMBUS_WRITE_I2C_BLOCK", |
| 124 | busStr, devAddr); |
| 125 | } |
| 126 | break; |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 127 | default: |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 128 | fprintf(stderr, "Unexpected write size type: %d\n", type); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 129 | assert(false); |
| 130 | } |
| 131 | } |
| 132 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 133 | void I2CDevice::open() |
| 134 | { |
| 135 | if (isOpen()) |
| 136 | { |
| 137 | throw I2CException("Device already open", busStr, devAddr); |
| 138 | } |
| 139 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 140 | int retries = 0; |
| 141 | do |
| 142 | { |
| 143 | fd = ::open(busStr.c_str(), O_RDWR); |
| 144 | } while ((fd == -1) && (++retries <= maxRetries)); |
| 145 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 146 | if (fd == -1) |
| 147 | { |
| 148 | throw I2CException("Failed to open", busStr, devAddr, errno); |
| 149 | } |
| 150 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 151 | retries = 0; |
| 152 | int ret = 0; |
| 153 | do |
| 154 | { |
| 155 | ret = ioctl(fd, I2C_SLAVE, devAddr); |
| 156 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 157 | |
| 158 | if (ret < 0) |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 159 | { |
| 160 | // Close device since setting slave address failed |
| 161 | closeWithoutException(); |
| 162 | |
| 163 | throw I2CException("Failed to set I2C_SLAVE", busStr, devAddr, errno); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void I2CDevice::close() |
| 168 | { |
| 169 | checkIsOpen(); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 170 | |
| 171 | int ret = 0, retries = 0; |
| 172 | do |
| 173 | { |
| 174 | ret = ::close(fd); |
| 175 | } while ((ret == -1) && (++retries <= maxRetries)); |
| 176 | |
| 177 | if (ret == -1) |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 178 | { |
| 179 | throw I2CException("Failed to close", busStr, devAddr, errno); |
| 180 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 181 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 182 | fd = INVALID_FD; |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 183 | cachedFuncs = NO_FUNCS; |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 184 | } |
| 185 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 186 | void I2CDevice::read(uint8_t& data) |
| 187 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 188 | checkIsOpen(); |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 189 | checkReadFuncs(I2C_SMBUS_BYTE); |
| 190 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 191 | int ret = 0, retries = 0; |
| 192 | do |
| 193 | { |
| 194 | ret = i2c_smbus_read_byte(fd); |
| 195 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 196 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 197 | if (ret < 0) |
| 198 | { |
| 199 | throw I2CException("Failed to read byte", busStr, devAddr, errno); |
| 200 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 201 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 202 | data = static_cast<uint8_t>(ret); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void I2CDevice::read(uint8_t addr, uint8_t& data) |
| 206 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 207 | checkIsOpen(); |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 208 | checkReadFuncs(I2C_SMBUS_BYTE_DATA); |
| 209 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 210 | int ret = 0, retries = 0; |
| 211 | do |
| 212 | { |
| 213 | ret = i2c_smbus_read_byte_data(fd, addr); |
| 214 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 215 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 216 | if (ret < 0) |
| 217 | { |
| 218 | throw I2CException("Failed to read byte data", busStr, devAddr, errno); |
| 219 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 220 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 221 | data = static_cast<uint8_t>(ret); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void I2CDevice::read(uint8_t addr, uint16_t& data) |
| 225 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 226 | checkIsOpen(); |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 227 | checkReadFuncs(I2C_SMBUS_WORD_DATA); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 228 | |
| 229 | int ret = 0, retries = 0; |
| 230 | do |
| 231 | { |
| 232 | ret = i2c_smbus_read_word_data(fd, addr); |
| 233 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 234 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 235 | if (ret < 0) |
| 236 | { |
| 237 | throw I2CException("Failed to read word data", busStr, devAddr, errno); |
| 238 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 239 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 240 | data = static_cast<uint16_t>(ret); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 243 | void I2CDevice::read(uint8_t addr, uint8_t& size, uint8_t* data, Mode mode) |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 244 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 245 | checkIsOpen(); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 246 | |
| 247 | int ret = -1, retries = 0; |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 248 | switch (mode) |
| 249 | { |
| 250 | case Mode::SMBUS: |
| 251 | checkReadFuncs(I2C_SMBUS_BLOCK_DATA); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 252 | do |
| 253 | { |
| 254 | ret = i2c_smbus_read_block_data(fd, addr, data); |
| 255 | } while ((ret < 0) && (++retries <= maxRetries)); |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 256 | break; |
| 257 | case Mode::I2C: |
| 258 | checkReadFuncs(I2C_SMBUS_I2C_BLOCK_DATA); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 259 | do |
| 260 | { |
| 261 | ret = i2c_smbus_read_i2c_block_data(fd, addr, size, data); |
| 262 | } while ((ret < 0) && (++retries <= maxRetries)); |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 263 | if (ret != size) |
| 264 | { |
| 265 | throw I2CException("Failed to read i2c block data", busStr, |
| 266 | devAddr, errno); |
| 267 | } |
| 268 | break; |
| 269 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 270 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 271 | if (ret < 0) |
| 272 | { |
| 273 | throw I2CException("Failed to read block data", busStr, devAddr, errno); |
| 274 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 275 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 276 | size = static_cast<uint8_t>(ret); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void I2CDevice::write(uint8_t data) |
| 280 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 281 | checkIsOpen(); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 282 | checkWriteFuncs(I2C_SMBUS_BYTE); |
| 283 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 284 | int ret = 0, retries = 0; |
| 285 | do |
| 286 | { |
| 287 | ret = i2c_smbus_write_byte(fd, data); |
| 288 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 289 | |
| 290 | if (ret < 0) |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 291 | { |
| 292 | throw I2CException("Failed to write byte", busStr, devAddr, errno); |
| 293 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void I2CDevice::write(uint8_t addr, uint8_t data) |
| 297 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 298 | checkIsOpen(); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 299 | checkWriteFuncs(I2C_SMBUS_BYTE_DATA); |
| 300 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 301 | int ret = 0, retries = 0; |
| 302 | do |
| 303 | { |
| 304 | ret = i2c_smbus_write_byte_data(fd, addr, data); |
| 305 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 306 | |
| 307 | if (ret < 0) |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 308 | { |
| 309 | throw I2CException("Failed to write byte data", busStr, devAddr, errno); |
| 310 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void I2CDevice::write(uint8_t addr, uint16_t data) |
| 314 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 315 | checkIsOpen(); |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 316 | checkWriteFuncs(I2C_SMBUS_WORD_DATA); |
| 317 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 318 | int ret = 0, retries = 0; |
| 319 | do |
| 320 | { |
| 321 | ret = i2c_smbus_write_word_data(fd, addr, data); |
| 322 | } while ((ret < 0) && (++retries <= maxRetries)); |
| 323 | |
| 324 | if (ret < 0) |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 325 | { |
| 326 | throw I2CException("Failed to write word data", busStr, devAddr, errno); |
| 327 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 328 | } |
| 329 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 330 | void I2CDevice::write(uint8_t addr, uint8_t size, const uint8_t* data, |
| 331 | Mode mode) |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 332 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 333 | checkIsOpen(); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 334 | |
| 335 | int ret = -1, retries = 0; |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 336 | switch (mode) |
| 337 | { |
| 338 | case Mode::SMBUS: |
| 339 | checkWriteFuncs(I2C_SMBUS_BLOCK_DATA); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 340 | do |
| 341 | { |
| 342 | ret = i2c_smbus_write_block_data(fd, addr, size, data); |
| 343 | } while ((ret < 0) && (++retries <= maxRetries)); |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 344 | break; |
| 345 | case Mode::I2C: |
| 346 | checkWriteFuncs(I2C_SMBUS_I2C_BLOCK_DATA); |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 347 | do |
| 348 | { |
| 349 | ret = i2c_smbus_write_i2c_block_data(fd, addr, size, data); |
| 350 | } while ((ret < 0) && (++retries <= maxRetries)); |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 351 | break; |
| 352 | } |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 353 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 354 | if (ret < 0) |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 355 | { |
| 356 | throw I2CException("Failed to write block data", busStr, devAddr, |
| 357 | errno); |
| 358 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 359 | } |
| 360 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 361 | std::unique_ptr<I2CInterface> I2CDevice::create(uint8_t busId, uint8_t devAddr, |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 362 | InitialState initialState, |
| 363 | int maxRetries) |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 364 | { |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 365 | std::unique_ptr<I2CDevice> dev( |
| 366 | new I2CDevice(busId, devAddr, initialState, maxRetries)); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 367 | return dev; |
| 368 | } |
| 369 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 370 | std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr, |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 371 | I2CInterface::InitialState initialState, |
| 372 | int maxRetries) |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 373 | { |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 374 | return I2CDevice::create(busId, devAddr, initialState, maxRetries); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | } // namespace i2c |