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 | * |
| 17 | * @param[in] busId - The i2c bus ID |
| 18 | * @param[in] devAddr - The device address of the I2C device |
| 19 | */ |
| 20 | explicit I2CDevice(uint8_t busId, uint8_t devAddr) : |
| 21 | busId(busId), devAddr(devAddr) |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 22 | { |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 23 | busStr = "/dev/i2c-" + std::to_string(busId); |
| 24 | open(); |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 25 | } |
| 26 | |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 27 | /** @brief Open the i2c device */ |
| 28 | void open(); |
| 29 | |
| 30 | /** @brief Close the i2c device */ |
| 31 | void close(); |
| 32 | |
| 33 | /** @brief The I2C bus ID */ |
| 34 | uint8_t busId; |
| 35 | |
| 36 | /** @brief The i2c device address in the bus */ |
| 37 | uint8_t devAddr; |
| 38 | |
| 39 | /** @brief The file descriptor of the opened i2c device */ |
| 40 | int fd; |
| 41 | |
| 42 | /** @brief The i2c bus path in /dev */ |
| 43 | std::string busStr; |
| 44 | |
Lei YU | 92e89eb | 2019-11-06 18:08:25 +0800 | [diff] [blame] | 45 | /** @brief Check i2c adapter read functionality |
| 46 | * |
| 47 | * Check if the i2c adapter has the functionality specified by the SMBus |
| 48 | * transaction type |
| 49 | * |
| 50 | * @param[in] type - The SMBus transaction type defined in linux/i2c.h |
| 51 | * |
| 52 | * @throw I2CException if the function is not supported |
| 53 | */ |
| 54 | void checkReadFuncs(int type); |
| 55 | |
Lei YU | 34fb8bd | 2019-11-07 14:24:20 +0800 | [diff] [blame^] | 56 | /** @brief Check i2c adapter write functionality |
| 57 | * |
| 58 | * Check if the i2c adapter has the functionality specified by the SMBus |
| 59 | * transaction type |
| 60 | * |
| 61 | * @param[in] type - The SMBus transaction type defined in linux/i2c.h |
| 62 | * |
| 63 | * @throw I2CException if the function is not supported |
| 64 | */ |
| 65 | void checkWriteFuncs(int type); |
| 66 | |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 67 | public: |
Lei YU | 9af82a5 | 2019-11-06 14:51:22 +0800 | [diff] [blame] | 68 | ~I2CDevice() |
| 69 | { |
| 70 | close(); |
| 71 | } |
Lei YU | ab1327c | 2019-11-04 16:53:39 +0800 | [diff] [blame] | 72 | |
| 73 | /** @copydoc I2CInterface::read(uint8_t&) */ |
| 74 | void read(uint8_t& data) override; |
| 75 | |
| 76 | /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */ |
| 77 | void read(uint8_t addr, uint8_t& data) override; |
| 78 | |
| 79 | /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */ |
| 80 | void read(uint8_t addr, uint16_t& data) override; |
| 81 | |
| 82 | /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*) */ |
| 83 | void read(uint8_t addr, uint8_t& size, uint8_t* data) override; |
| 84 | |
| 85 | /** @copydoc I2CInterface::write(uint8_t) */ |
| 86 | void write(uint8_t data) override; |
| 87 | |
| 88 | /** @copydoc I2CInterface::write(uint8_t,uint8_t) */ |
| 89 | void write(uint8_t addr, uint8_t data) override; |
| 90 | |
| 91 | /** @copydoc I2CInterface::write(uint8_t,uint16_t) */ |
| 92 | void write(uint8_t addr, uint16_t data) override; |
| 93 | |
| 94 | /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*) */ |
| 95 | void write(uint8_t addr, uint8_t size, const uint8_t* data) override; |
| 96 | |
| 97 | /** @brief Create an I2CInterface instance |
| 98 | * |
| 99 | * @param[in] busId - The i2c bus ID |
| 100 | * @param[in] devAddr - The device address of the i2c |
| 101 | * |
| 102 | * @return The unique_ptr holding the I2CInterface |
| 103 | */ |
| 104 | static std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr); |
| 105 | }; |
| 106 | |
| 107 | } // namespace i2c |