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