Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "i2c_interface.hpp" |
| 4 | |
| 5 | namespace i2c |
| 6 | { |
| 7 | |
| 8 | class I2CDevice : public I2CInterface |
| 9 | { |
| 10 | private: |
| 11 | I2CDevice() = delete; |
| 12 | |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 13 | /** @brief Constructor |
| 14 | * |
| 15 | * Construct I2CDevice object from the bus id and device address |
| 16 | * |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 17 | * Automatically opens the I2CDevice if initialState is OPEN. |
| 18 | * |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 19 | * @param[in] busId - The i2c bus ID |
| 20 | * @param[in] devAddr - The device address of the I2C device |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 21 | * @param[in] initialState - Initial state of the I2CDevice object |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 22 | * @param[in] maxRetries - Maximum number of times to retry an I2C operation |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 23 | */ |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 24 | explicit I2CDevice(uint8_t busId, uint8_t devAddr, |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 25 | InitialState initialState = InitialState::OPEN, |
| 26 | int maxRetries = 0) : |
Jayanth Othayoth | 28a2ca4 | 2024-12-13 05:12:31 -0600 | [diff] [blame] | 27 | devAddr(devAddr), maxRetries(maxRetries), |
George Liu | b9cf0d2 | 2023-02-28 10:32:42 +0800 | [diff] [blame] | 28 | busStr("/dev/i2c-" + std::to_string(busId)) |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 29 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 30 | if (initialState == InitialState::OPEN) |
| 31 | { |
| 32 | open(); |
| 33 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 34 | } |
| 35 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 36 | /** @brief Invalid file descriptor */ |
| 37 | static constexpr int INVALID_FD = -1; |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 38 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 39 | /** @brief Empty adapter functionality value with no bit flags set */ |
| 40 | static constexpr unsigned long NO_FUNCS = 0; |
| 41 | |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 42 | /** @brief The i2c device address in the bus */ |
| 43 | uint8_t devAddr; |
| 44 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 45 | /** @brief Maximum number of times to retry an I2C operation */ |
| 46 | int maxRetries = 0; |
| 47 | |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 48 | /** @brief The file descriptor of the opened i2c device */ |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 49 | int fd = INVALID_FD; |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 50 | |
| 51 | /** @brief The i2c bus path in /dev */ |
| 52 | std::string busStr; |
| 53 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 54 | /** @brief Cached I2C adapter functionality value */ |
| 55 | unsigned long cachedFuncs = NO_FUNCS; |
| 56 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 57 | /** @brief Check that device interface is open |
| 58 | * |
| 59 | * @throw I2CException if device is not open |
| 60 | */ |
| 61 | void checkIsOpen() const |
| 62 | { |
| 63 | if (!isOpen()) |
| 64 | { |
| 65 | throw I2CException("Device not open", busStr, devAddr); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** @brief Close device without throwing an exception if an error occurs */ |
| 70 | void closeWithoutException() noexcept |
| 71 | { |
| 72 | try |
| 73 | { |
| 74 | close(); |
| 75 | } |
| 76 | catch (...) |
Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 77 | {} |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 80 | /** @brief Get I2C adapter functionality |
| 81 | * |
| 82 | * Caches the adapter functionality value since it shouldn't change after |
| 83 | * opening the device. |
| 84 | * |
| 85 | * @throw I2CException on error |
| 86 | * @return Adapter functionality value |
| 87 | */ |
| 88 | unsigned long getFuncs(); |
| 89 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 90 | /** @brief Check i2c adapter read functionality |
| 91 | * |
| 92 | * Check if the i2c adapter has the functionality specified by the SMBus |
| 93 | * transaction type |
| 94 | * |
| 95 | * @param[in] type - The SMBus transaction type defined in linux/i2c.h |
| 96 | * |
| 97 | * @throw I2CException if the function is not supported |
| 98 | */ |
| 99 | void checkReadFuncs(int type); |
| 100 | |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 101 | /** @brief Check i2c adapter write functionality |
| 102 | * |
| 103 | * Check if the i2c adapter has the functionality specified by the SMBus |
| 104 | * transaction type |
| 105 | * |
| 106 | * @param[in] type - The SMBus transaction type defined in linux/i2c.h |
| 107 | * |
| 108 | * @throw I2CException if the function is not supported |
| 109 | */ |
| 110 | void checkWriteFuncs(int type); |
| 111 | |
Shawn McCarney | a3ff7e7 | 2024-10-15 17:34:49 -0500 | [diff] [blame] | 112 | /** @brief SMBus Block Write-Block Read Process Call using SMBus function |
| 113 | * |
| 114 | * In SMBus 2.0 the maximum write size + read size is <= 32 bytes. |
| 115 | * In SMBus 3.0 the maximum write size + read size is <= 255 bytes. |
| 116 | * The Linux SMBus function currently only supports the SMBus 2.0 maximum. |
| 117 | * |
| 118 | * @param[in] addr - The register address of the i2c device |
| 119 | * @param[in] writeSize - The size of data to write. Write size + read size |
| 120 | * must be <= 32 bytes. |
| 121 | * @param[in] writeData - The data to write to the i2c device |
| 122 | * @param[out] readSize - The size of data read from i2c device. Write size |
| 123 | * + read size must be <= 32 bytes. |
| 124 | * @param[out] readData - Pointer to buffer to hold the data read from the |
| 125 | * i2c device. Must be large enough to hold the data |
| 126 | * returned by the device (max is 32 bytes). |
| 127 | * |
| 128 | * @throw I2CException on error |
| 129 | */ |
| 130 | void processCallSMBus(uint8_t addr, uint8_t writeSize, |
| 131 | const uint8_t* writeData, uint8_t& readSize, |
| 132 | uint8_t* readData); |
| 133 | |
| 134 | /** @brief SMBus Block Write-Block Read Process Call using I2C messages |
| 135 | * |
| 136 | * This method supports block writes of more than 32 bytes. It can also be |
| 137 | * used with I2C adapters that do not support the block process call |
| 138 | * protocol but do support I2C-level commands. |
| 139 | * |
| 140 | * This method implements the block process call using the lower level |
| 141 | * I2C_RDWR ioctl to send I2C messages. Using this ioctl allows for writes |
| 142 | * up to 255 bytes. The write size + read size must be <= 255 bytes. |
| 143 | * |
| 144 | * @param[in] addr - The register address of the i2c device |
| 145 | * @param[in] writeSize - The size of data to write. Write size + read size |
| 146 | * must be <= 255 bytes. |
| 147 | * @param[in] writeData - The data to write to the i2c device |
| 148 | * @param[out] readSize - The size of data read from i2c device. Max read |
| 149 | * size is 32 bytes, and write size + read size must |
| 150 | * be <= 255 bytes. |
| 151 | * @param[out] readData - Pointer to buffer to hold the data read from the |
| 152 | * i2c device. Must be large enough to hold the data |
| 153 | * returned by the device (max is 32 bytes). |
| 154 | * |
| 155 | * @throw I2CException on error |
| 156 | */ |
| 157 | void processCallI2C(uint8_t addr, uint8_t writeSize, |
| 158 | const uint8_t* writeData, uint8_t& readSize, |
| 159 | uint8_t* readData); |
| 160 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 161 | public: |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 162 | /** @copydoc I2CInterface::~I2CInterface() */ |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 163 | ~I2CDevice() |
| 164 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 165 | if (isOpen()) |
| 166 | { |
| 167 | // Note: destructors must not throw exceptions |
| 168 | closeWithoutException(); |
| 169 | } |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 170 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 171 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 172 | /** @copydoc I2CInterface::open() */ |
Jayanth Othayoth | 12c4a42 | 2024-12-06 11:31:34 -0600 | [diff] [blame] | 173 | void open() override; |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 174 | |
| 175 | /** @copydoc I2CInterface::isOpen() */ |
Jayanth Othayoth | 12c4a42 | 2024-12-06 11:31:34 -0600 | [diff] [blame] | 176 | bool isOpen() const override |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 177 | { |
| 178 | return (fd != INVALID_FD); |
| 179 | } |
| 180 | |
| 181 | /** @copydoc I2CInterface::close() */ |
Jayanth Othayoth | 12c4a42 | 2024-12-06 11:31:34 -0600 | [diff] [blame] | 182 | void close() override; |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 183 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 184 | /** @copydoc I2CInterface::read(uint8_t&) */ |
| 185 | void read(uint8_t& data) override; |
| 186 | |
| 187 | /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */ |
| 188 | void read(uint8_t addr, uint8_t& data) override; |
| 189 | |
| 190 | /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */ |
| 191 | void read(uint8_t addr, uint16_t& data) override; |
| 192 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 193 | /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */ |
| 194 | void read(uint8_t addr, uint8_t& size, uint8_t* data, |
| 195 | Mode mode = Mode::SMBUS) override; |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 196 | |
| 197 | /** @copydoc I2CInterface::write(uint8_t) */ |
| 198 | void write(uint8_t data) override; |
| 199 | |
| 200 | /** @copydoc I2CInterface::write(uint8_t,uint8_t) */ |
| 201 | void write(uint8_t addr, uint8_t data) override; |
| 202 | |
| 203 | /** @copydoc I2CInterface::write(uint8_t,uint16_t) */ |
| 204 | void write(uint8_t addr, uint16_t data) override; |
| 205 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 206 | /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */ |
| 207 | void write(uint8_t addr, uint8_t size, const uint8_t* data, |
| 208 | Mode mode = Mode::SMBUS) override; |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 209 | |
Shawn McCarney | a3ff7e7 | 2024-10-15 17:34:49 -0500 | [diff] [blame] | 210 | /** @copydoc I2CInterface::processCall(uint8_t,uint16_t,uint16_t&) */ |
| 211 | void processCall(uint8_t addr, uint16_t writeData, |
| 212 | uint16_t& readData) override; |
| 213 | |
| 214 | /** @copydoc I2CInterface::processCall(uint8_t,uint8_t,const |
| 215 | * uint8_t*,uint8_t&,uint8_t*) */ |
| 216 | void processCall(uint8_t addr, uint8_t writeSize, const uint8_t* writeData, |
| 217 | uint8_t& readSize, uint8_t* readData) override; |
| 218 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 219 | /** @brief Create an I2CInterface instance |
| 220 | * |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 221 | * Automatically opens the I2CInterface if initialState is OPEN. |
| 222 | * |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 223 | * @param[in] busId - The i2c bus ID |
| 224 | * @param[in] devAddr - The device address of the i2c |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 225 | * @param[in] initialState - Initial state of the I2CInterface object |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 226 | * @param[in] maxRetries - Maximum number of times to retry an I2C operation |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 227 | * |
| 228 | * @return The unique_ptr holding the I2CInterface |
| 229 | */ |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 230 | static std::unique_ptr<I2CInterface> |
| 231 | create(uint8_t busId, uint8_t devAddr, |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 232 | InitialState initialState = InitialState::OPEN, |
| 233 | int maxRetries = 0); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | } // namespace i2c |