blob: 4e430655d60a03290e7e17b4429e1cc838648f7f [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 *
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060017 * Automatically opens the I2CDevice if initialState is OPEN.
18 *
Lei YU9af82a52019-11-06 14:51:22 +080019 * @param[in] busId - The i2c bus ID
20 * @param[in] devAddr - The device address of the I2C device
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060021 * @param[in] initialState - Initial state of the I2CDevice object
Shawn McCarney770de582021-11-05 02:28:35 -050022 * @param[in] maxRetries - Maximum number of times to retry an I2C operation
Lei YU9af82a52019-11-06 14:51:22 +080023 */
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060024 explicit I2CDevice(uint8_t busId, uint8_t devAddr,
Shawn McCarney770de582021-11-05 02:28:35 -050025 InitialState initialState = InitialState::OPEN,
26 int maxRetries = 0) :
Patrick Williamsf5402192024-08-16 15:20:53 -040027 busId(busId), devAddr(devAddr), maxRetries(maxRetries),
George Liub9cf0d22023-02-28 10:32:42 +080028 busStr("/dev/i2c-" + std::to_string(busId))
Lei YUab1327c2019-11-04 16:53:39 +080029 {
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060030 if (initialState == InitialState::OPEN)
31 {
32 open();
33 }
Lei YUab1327c2019-11-04 16:53:39 +080034 }
35
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060036 /** @brief Invalid file descriptor */
37 static constexpr int INVALID_FD = -1;
Lei YU9af82a52019-11-06 14:51:22 +080038
Shawn McCarney38ed88d2019-12-11 12:26:09 -060039 /** @brief Empty adapter functionality value with no bit flags set */
40 static constexpr unsigned long NO_FUNCS = 0;
41
Lei YU9af82a52019-11-06 14:51:22 +080042 /** @brief The I2C bus ID */
43 uint8_t busId;
44
45 /** @brief The i2c device address in the bus */
46 uint8_t devAddr;
47
Shawn McCarney770de582021-11-05 02:28:35 -050048 /** @brief Maximum number of times to retry an I2C operation */
49 int maxRetries = 0;
50
Lei YU9af82a52019-11-06 14:51:22 +080051 /** @brief The file descriptor of the opened i2c device */
Shawn McCarney38ed88d2019-12-11 12:26:09 -060052 int fd = INVALID_FD;
Lei YU9af82a52019-11-06 14:51:22 +080053
54 /** @brief The i2c bus path in /dev */
55 std::string busStr;
56
Shawn McCarney38ed88d2019-12-11 12:26:09 -060057 /** @brief Cached I2C adapter functionality value */
58 unsigned long cachedFuncs = NO_FUNCS;
59
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060060 /** @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 (...)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000080 {}
Shawn McCarneyd45a9a62019-12-10 18:35:44 -060081 }
82
Shawn McCarney38ed88d2019-12-11 12:26:09 -060083 /** @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
Lei YU92e89eb2019-11-06 18:08:25 +080093 /** @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
Lei YU34fb8bd2019-11-07 14:24:20 +0800104 /** @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
Shawn McCarneya3ff7e72024-10-15 17:34:49 -0500115 /** @brief SMBus Block Write-Block Read Process Call using SMBus function
116 *
117 * In SMBus 2.0 the maximum write size + read size is <= 32 bytes.
118 * In SMBus 3.0 the maximum write size + read size is <= 255 bytes.
119 * The Linux SMBus function currently only supports the SMBus 2.0 maximum.
120 *
121 * @param[in] addr - The register address of the i2c device
122 * @param[in] writeSize - The size of data to write. Write size + read size
123 * must be <= 32 bytes.
124 * @param[in] writeData - The data to write to the i2c device
125 * @param[out] readSize - The size of data read from i2c device. Write size
126 * + read size must be <= 32 bytes.
127 * @param[out] readData - Pointer to buffer to hold the data read from the
128 * i2c device. Must be large enough to hold the data
129 * returned by the device (max is 32 bytes).
130 *
131 * @throw I2CException on error
132 */
133 void processCallSMBus(uint8_t addr, uint8_t writeSize,
134 const uint8_t* writeData, uint8_t& readSize,
135 uint8_t* readData);
136
137 /** @brief SMBus Block Write-Block Read Process Call using I2C messages
138 *
139 * This method supports block writes of more than 32 bytes. It can also be
140 * used with I2C adapters that do not support the block process call
141 * protocol but do support I2C-level commands.
142 *
143 * This method implements the block process call using the lower level
144 * I2C_RDWR ioctl to send I2C messages. Using this ioctl allows for writes
145 * up to 255 bytes. The write size + read size must be <= 255 bytes.
146 *
147 * @param[in] addr - The register address of the i2c device
148 * @param[in] writeSize - The size of data to write. Write size + read size
149 * must be <= 255 bytes.
150 * @param[in] writeData - The data to write to the i2c device
151 * @param[out] readSize - The size of data read from i2c device. Max read
152 * size is 32 bytes, and write size + read size must
153 * be <= 255 bytes.
154 * @param[out] readData - Pointer to buffer to hold the data read from the
155 * i2c device. Must be large enough to hold the data
156 * returned by the device (max is 32 bytes).
157 *
158 * @throw I2CException on error
159 */
160 void processCallI2C(uint8_t addr, uint8_t writeSize,
161 const uint8_t* writeData, uint8_t& readSize,
162 uint8_t* readData);
163
Lei YUab1327c2019-11-04 16:53:39 +0800164 public:
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600165 /** @copydoc I2CInterface::~I2CInterface() */
Lei YU9af82a52019-11-06 14:51:22 +0800166 ~I2CDevice()
167 {
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600168 if (isOpen())
169 {
170 // Note: destructors must not throw exceptions
171 closeWithoutException();
172 }
Lei YU9af82a52019-11-06 14:51:22 +0800173 }
Lei YUab1327c2019-11-04 16:53:39 +0800174
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600175 /** @copydoc I2CInterface::open() */
Jayanth Othayoth12c4a422024-12-06 11:31:34 -0600176 void open() override;
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600177
178 /** @copydoc I2CInterface::isOpen() */
Jayanth Othayoth12c4a422024-12-06 11:31:34 -0600179 bool isOpen() const override
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600180 {
181 return (fd != INVALID_FD);
182 }
183
184 /** @copydoc I2CInterface::close() */
Jayanth Othayoth12c4a422024-12-06 11:31:34 -0600185 void close() override;
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600186
Lei YUab1327c2019-11-04 16:53:39 +0800187 /** @copydoc I2CInterface::read(uint8_t&) */
188 void read(uint8_t& data) override;
189
190 /** @copydoc I2CInterface::read(uint8_t,uint8_t&) */
191 void read(uint8_t addr, uint8_t& data) override;
192
193 /** @copydoc I2CInterface::read(uint8_t,uint16_t&) */
194 void read(uint8_t addr, uint16_t& data) override;
195
Lei YU1d103422019-11-29 14:00:02 +0800196 /** @copydoc I2CInterface::read(uint8_t,uint8_t&,uint8_t*,Mode) */
197 void read(uint8_t addr, uint8_t& size, uint8_t* data,
198 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +0800199
200 /** @copydoc I2CInterface::write(uint8_t) */
201 void write(uint8_t data) override;
202
203 /** @copydoc I2CInterface::write(uint8_t,uint8_t) */
204 void write(uint8_t addr, uint8_t data) override;
205
206 /** @copydoc I2CInterface::write(uint8_t,uint16_t) */
207 void write(uint8_t addr, uint16_t data) override;
208
Lei YU1d103422019-11-29 14:00:02 +0800209 /** @copydoc I2CInterface::write(uint8_t,uint8_t,const uint8_t*,Mode) */
210 void write(uint8_t addr, uint8_t size, const uint8_t* data,
211 Mode mode = Mode::SMBUS) override;
Lei YUab1327c2019-11-04 16:53:39 +0800212
Shawn McCarneya3ff7e72024-10-15 17:34:49 -0500213 /** @copydoc I2CInterface::processCall(uint8_t,uint16_t,uint16_t&) */
214 void processCall(uint8_t addr, uint16_t writeData,
215 uint16_t& readData) override;
216
217 /** @copydoc I2CInterface::processCall(uint8_t,uint8_t,const
218 * uint8_t*,uint8_t&,uint8_t*) */
219 void processCall(uint8_t addr, uint8_t writeSize, const uint8_t* writeData,
220 uint8_t& readSize, uint8_t* readData) override;
221
Lei YUab1327c2019-11-04 16:53:39 +0800222 /** @brief Create an I2CInterface instance
223 *
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600224 * Automatically opens the I2CInterface if initialState is OPEN.
225 *
Lei YUab1327c2019-11-04 16:53:39 +0800226 * @param[in] busId - The i2c bus ID
227 * @param[in] devAddr - The device address of the i2c
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600228 * @param[in] initialState - Initial state of the I2CInterface object
Shawn McCarney770de582021-11-05 02:28:35 -0500229 * @param[in] maxRetries - Maximum number of times to retry an I2C operation
Lei YUab1327c2019-11-04 16:53:39 +0800230 *
231 * @return The unique_ptr holding the I2CInterface
232 */
Shawn McCarneyd45a9a62019-12-10 18:35:44 -0600233 static std::unique_ptr<I2CInterface>
234 create(uint8_t busId, uint8_t devAddr,
Shawn McCarney770de582021-11-05 02:28:35 -0500235 InitialState initialState = InitialState::OPEN,
236 int maxRetries = 0);
Lei YUab1327c2019-11-04 16:53:39 +0800237};
238
239} // namespace i2c