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) : |
Patrick Williams | f540219 | 2024-08-16 15:20:53 -0400 | [diff] [blame^] | 27 | busId(busId), 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 bus ID */ |
| 43 | uint8_t busId; |
| 44 | |
| 45 | /** @brief The i2c device address in the bus */ |
| 46 | uint8_t devAddr; |
| 47 | |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 48 | /** @brief Maximum number of times to retry an I2C operation */ |
| 49 | int maxRetries = 0; |
| 50 | |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 51 | /** @brief The file descriptor of the opened i2c device */ |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 52 | int fd = INVALID_FD; |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 53 | |
| 54 | /** @brief The i2c bus path in /dev */ |
| 55 | std::string busStr; |
| 56 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 57 | /** @brief Cached I2C adapter functionality value */ |
| 58 | unsigned long cachedFuncs = NO_FUNCS; |
| 59 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 60 | /** @brief Check that device interface is open |
| 61 | * |
| 62 | * @throw I2CException if device is not open |
| 63 | */ |
| 64 | void checkIsOpen() const |
| 65 | { |
| 66 | if (!isOpen()) |
| 67 | { |
| 68 | throw I2CException("Device not open", busStr, devAddr); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** @brief Close device without throwing an exception if an error occurs */ |
| 73 | void closeWithoutException() noexcept |
| 74 | { |
| 75 | try |
| 76 | { |
| 77 | close(); |
| 78 | } |
| 79 | catch (...) |
Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 80 | {} |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 81 | } |
| 82 | |
Shawn McCarney | 38ed88d | 2019-12-11 12:26:09 -0600 | [diff] [blame] | 83 | /** @brief Get I2C adapter functionality |
| 84 | * |
| 85 | * Caches the adapter functionality value since it shouldn't change after |
| 86 | * opening the device. |
| 87 | * |
| 88 | * @throw I2CException on error |
| 89 | * @return Adapter functionality value |
| 90 | */ |
| 91 | unsigned long getFuncs(); |
| 92 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 93 | /** @brief Check i2c adapter read functionality |
| 94 | * |
| 95 | * Check if the i2c adapter has the functionality specified by the SMBus |
| 96 | * transaction type |
| 97 | * |
| 98 | * @param[in] type - The SMBus transaction type defined in linux/i2c.h |
| 99 | * |
| 100 | * @throw I2CException if the function is not supported |
| 101 | */ |
| 102 | void checkReadFuncs(int type); |
| 103 | |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame] | 104 | /** @brief Check i2c adapter write functionality |
| 105 | * |
| 106 | * Check if the i2c adapter has the functionality specified by the SMBus |
| 107 | * transaction type |
| 108 | * |
| 109 | * @param[in] type - The SMBus transaction type defined in linux/i2c.h |
| 110 | * |
| 111 | * @throw I2CException if the function is not supported |
| 112 | */ |
| 113 | void checkWriteFuncs(int type); |
| 114 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 115 | public: |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 116 | /** @copydoc I2CInterface::~I2CInterface() */ |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 117 | ~I2CDevice() |
| 118 | { |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 119 | if (isOpen()) |
| 120 | { |
| 121 | // Note: destructors must not throw exceptions |
| 122 | closeWithoutException(); |
| 123 | } |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 124 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 125 | |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 126 | /** @copydoc I2CInterface::open() */ |
| 127 | void open(); |
| 128 | |
| 129 | /** @copydoc I2CInterface::isOpen() */ |
| 130 | bool isOpen() const |
| 131 | { |
| 132 | return (fd != INVALID_FD); |
| 133 | } |
| 134 | |
| 135 | /** @copydoc I2CInterface::close() */ |
| 136 | void close(); |
| 137 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 138 | /** @copydoc I2CInterface::read(uint8_t&) */ |
| 139 | void read(uint8_t& data) override; |
| 140 | |
| 141 | /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */ |
| 142 | void read(uint8_t addr, uint8_t& data) override; |
| 143 | |
| 144 | /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */ |
| 145 | void read(uint8_t addr, uint16_t& data) override; |
| 146 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 147 | /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */ |
| 148 | void read(uint8_t addr, uint8_t& size, uint8_t* data, |
| 149 | Mode mode = Mode::SMBUS) override; |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 150 | |
| 151 | /** @copydoc I2CInterface::write(uint8_t) */ |
| 152 | void write(uint8_t data) override; |
| 153 | |
| 154 | /** @copydoc I2CInterface::write(uint8_t,uint8_t) */ |
| 155 | void write(uint8_t addr, uint8_t data) override; |
| 156 | |
| 157 | /** @copydoc I2CInterface::write(uint8_t,uint16_t) */ |
| 158 | void write(uint8_t addr, uint16_t data) override; |
| 159 | |
Lei YU | 1d10342 | 2019-11-29 14:00:02 +0800 | [diff] [blame] | 160 | /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */ |
| 161 | void write(uint8_t addr, uint8_t size, const uint8_t* data, |
| 162 | Mode mode = Mode::SMBUS) override; |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 163 | |
| 164 | /** @brief Create an I2CInterface instance |
| 165 | * |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 166 | * Automatically opens the I2CInterface if initialState is OPEN. |
| 167 | * |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 168 | * @param[in] busId - The i2c bus ID |
| 169 | * @param[in] devAddr - The device address of the i2c |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 170 | * @param[in] initialState - Initial state of the I2CInterface object |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 171 | * @param[in] maxRetries - Maximum number of times to retry an I2C operation |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 172 | * |
| 173 | * @return The unique_ptr holding the I2CInterface |
| 174 | */ |
Shawn McCarney | d45a9a6 | 2019-12-10 18:35:44 -0600 | [diff] [blame] | 175 | static std::unique_ptr<I2CInterface> |
| 176 | create(uint8_t busId, uint8_t devAddr, |
Shawn McCarney | 770de58 | 2021-11-05 02:28:35 -0500 | [diff] [blame] | 177 | InitialState initialState = InitialState::OPEN, |
| 178 | int maxRetries = 0); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace i2c |