blob: 7fe064f966ef0b7524116191d409200fe07f0e83 [file] [log] [blame]
Lei YUab1327c2019-11-04 16:53:39 +08001#pragma once
2
3#include "i2c_interface.hpp"
4
5namespace i2c
6{
7
8class I2CDevice : public I2CInterface
9{
10 private:
11 I2CDevice() = delete;
12
Lei YU9af82a52019-11-06 14:51:22 +080013 /** @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 YUab1327c2019-11-04 16:53:39 +080022 {
Lei YU9af82a52019-11-06 14:51:22 +080023 busStr = "/dev/i2c-" + std::to_string(busId);
24 open();
Lei YUab1327c2019-11-04 16:53:39 +080025 }
26
Lei YU9af82a52019-11-06 14:51:22 +080027 /** @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 YU92e89eb2019-11-06 18:08:25 +080045 /** @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 YUab1327c2019-11-04 16:53:39 +080056 public:
Lei YU9af82a52019-11-06 14:51:22 +080057 ~I2CDevice()
58 {
59 close();
60 }
Lei YUab1327c2019-11-04 16:53:39 +080061
62 /** @copydoc I2CInterface::read(uint8_t&) */
63 void read(uint8_t& data) override;
64
65 /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */
66 void read(uint8_t addr, uint8_t& data) override;
67
68 /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */
69 void read(uint8_t addr, uint16_t& data) override;
70
71 /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*) */
72 void read(uint8_t addr, uint8_t& size, uint8_t* data) override;
73
74 /** @copydoc I2CInterface::write(uint8_t) */
75 void write(uint8_t data) override;
76
77 /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
78 void write(uint8_t addr, uint8_t data) override;
79
80 /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
81 void write(uint8_t addr, uint16_t data) override;
82
83 /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*) */
84 void write(uint8_t addr, uint8_t size, const uint8_t* data) override;
85
86 /** @brief Create an I2CInterface instance
87 *
88 * @param[in] busId - The i2c bus ID
89 * @param[in] devAddr - The device address of the i2c
90 *
91 * @return The unique_ptr holding the I2CInterface
92 */
93 static std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
94};
95
96} // namespace i2c