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 13 /** @brief Constructor 14 * 15 * Construct I2CDevice object from the bus id and device address 16 * 17 * Automatically opens the I2CDevice if initialState is OPEN. 18 * 19 * @param[in] busId - The i2c bus ID 20 * @param[in] devAddr - The device address of the I2C device 21 * @param[in] initialState - Initial state of the I2CDevice object 22 * @param[in] maxRetries - Maximum number of times to retry an I2C operation 23 */ 24 explicit I2CDevice(uint8_t busId, uint8_t devAddr, 25 InitialState initialState = InitialState::OPEN, 26 int maxRetries = 0) : 27 busId(busId), devAddr(devAddr), maxRetries(maxRetries), 28 busStr("/dev/i2c-" + std::to_string(busId)) 29 { 30 if (initialState == InitialState::OPEN) 31 { 32 open(); 33 } 34 } 35 36 /** @brief Invalid file descriptor */ 37 static constexpr int INVALID_FD = -1; 38 39 /** @brief Empty adapter functionality value with no bit flags set */ 40 static constexpr unsigned long NO_FUNCS = 0; 41 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 48 /** @brief Maximum number of times to retry an I2C operation */ 49 int maxRetries = 0; 50 51 /** @brief The file descriptor of the opened i2c device */ 52 int fd = INVALID_FD; 53 54 /** @brief The i2c bus path in /dev */ 55 std::string busStr; 56 57 /** @brief Cached I2C adapter functionality value */ 58 unsigned long cachedFuncs = NO_FUNCS; 59 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 (...) 80 {} 81 } 82 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 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 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 115 public: 116 /** @copydoc I2CInterface::~I2CInterface() */ 117 ~I2CDevice() 118 { 119 if (isOpen()) 120 { 121 // Note: destructors must not throw exceptions 122 closeWithoutException(); 123 } 124 } 125 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 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 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; 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 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; 163 164 /** @brief Create an I2CInterface instance 165 * 166 * Automatically opens the I2CInterface if initialState is OPEN. 167 * 168 * @param[in] busId - The i2c bus ID 169 * @param[in] devAddr - The device address of the i2c 170 * @param[in] initialState - Initial state of the I2CInterface object 171 * @param[in] maxRetries - Maximum number of times to retry an I2C operation 172 * 173 * @return The unique_ptr holding the I2CInterface 174 */ 175 static std::unique_ptr<I2CInterface> 176 create(uint8_t busId, uint8_t devAddr, 177 InitialState initialState = InitialState::OPEN, 178 int maxRetries = 0); 179 }; 180 181 } // namespace i2c 182