blob: 05b02b589d9b0347c4be9b676a89650777e4c716 [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 YU34fb8bd2019-11-07 14:24:20 +080056 /** @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 YUab1327c2019-11-04 16:53:39 +080067 public:
Lei YU9af82a52019-11-06 14:51:22 +080068 ~I2CDevice()
69 {
70 close();
71 }
Lei YUab1327c2019-11-04 16:53:39 +080072
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
Lei YU1d103422019-11-29 14:00:02 +080082 /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */
83 void read(uint8_t addr, uint8_t& size, uint8_t* data,
84 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +080085
86 /** @copydoc I2CInterface::write(uint8_t) */
87 void write(uint8_t data) override;
88
89 /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
90 void write(uint8_t addr, uint8_t data) override;
91
92 /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
93 void write(uint8_t addr, uint16_t data) override;
94
Lei YU1d103422019-11-29 14:00:02 +080095 /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */
96 void write(uint8_t addr, uint8_t size, const uint8_t* data,
97 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +080098
99 /** @brief Create an I2CInterface instance
100 *
101 * @param[in] busId - The i2c bus ID
102 * @param[in] devAddr - The device address of the i2c
103 *
104 * @return The unique_ptr holding the I2CInterface
105 */
106 static std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
107};
108
109} // namespace i2c