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