blob: ab12e32f05864dde10114be9a2e64191a9cf26db [file] [log] [blame]
Lei YUab1327c2019-11-04 16:53:39 +08001#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <exception>
6#include <iostream>
7#include <memory>
8#include <sstream>
9#include <string>
10#include <vector>
11
12namespace i2c
13{
14
15class I2CException : public std::exception
16{
17 public:
18 explicit I2CException(const std::string& info, const std::string& bus,
19 uint8_t addr, int errorCode = 0) :
20 bus(bus),
21 addr(addr), errorCode(errorCode)
22 {
23 std::stringstream ss;
24 ss << "I2CException: " << info << ": bus " << bus << ", addr 0x"
25 << std::hex << static_cast<int>(addr);
26 if (errorCode != 0)
27 {
28 ss << std::dec << ", errno " << errorCode << ": "
29 << strerror(errorCode);
30 }
31 errStr = ss.str();
32 }
33 virtual ~I2CException() = default;
34
35 const char* what() const noexcept override
36 {
37 return errStr.c_str();
38 }
39 std::string bus;
40 uint8_t addr;
41 int errorCode;
42 std::string errStr;
43};
44
45class I2CInterface
46{
47 public:
48 virtual ~I2CInterface() = default;
49
50 /** @brief Read byte data from i2c
51 *
52 * @param[out] data - The data read from the i2c device
53 *
54 * @throw I2CException on error
55 */
56 virtual void read(uint8_t& data) = 0;
57
58 /** @brief Read byte data from i2c
59 *
60 * @param[in] addr - The register address of the i2c device
61 * @param[out] data - The data read from the i2c device
62 *
63 * @throw I2CException on error
64 */
65 virtual void read(uint8_t addr, uint8_t& data) = 0;
66
67 /** @brief Read word data from i2c
68 *
69 * @param[in] addr - The register address of the i2c device
70 * @param[out] data - The data read from the i2c device
71 *
72 * @throw I2CException on error
73 */
74 virtual void read(uint8_t addr, uint16_t& data) = 0;
75
76 /** @brief Read block data from i2c
77 *
78 * @param[in] addr - The register address of the i2c device
79 * @param[out] size - The size of data read from i2c device
80 * @param[out] data - The pointer to buffer to read from the i2c device,
81 * the buffer shall be big enough to hold the data
82 * returned by the device. SMBus allows at most 32
83 * bytes.
84 *
85 * @throw I2CException on error
86 */
87 virtual void read(uint8_t addr, uint8_t& size, uint8_t* data) = 0;
88
89 /** @brief Write byte data to i2c
90 *
91 * @param[in] data - The data to write to the i2c device
92 *
93 * @throw I2CException on error
94 */
95 virtual void write(uint8_t data) = 0;
96
97 /** @brief Write byte data to i2c
98 *
99 * @param[in] addr - The register address of the i2c device
100 * @param[in] data - The data to write to the i2c device
101 *
102 * @throw I2CException on error
103 */
104 virtual void write(uint8_t addr, uint8_t data) = 0;
105
106 /** @brief Write word data to i2c
107 *
108 * @param[in] addr - The register address of the i2c device
109 * @param[in] data - The data to write to the i2c device
110 *
111 * @throw I2CException on error
112 */
113 virtual void write(uint8_t addr, uint16_t data) = 0;
114
115 /** @brief Write block data to i2c
116 *
117 * @param[in] addr - The register address of the i2c device
118 * @param[in] size - The size of data to write, SMBus allows at most 32
119 * bytes
120 * @param[in] data - The data to write to the i2c device
121 *
122 * @throw I2CException on error
123 */
124 virtual void write(uint8_t addr, uint8_t size, const uint8_t* data) = 0;
125};
126
127/** @brief Create an I2CInterface instance
128 *
129 * @param[in] busId - The i2c bus ID
130 * @param[in] devAddr - The device address of the i2c
131 *
132 * @return The unique_ptr holding the I2CInterface
133 */
134std::unique_ptr<I2CInterface> create(uint8_t busId, uint8_t devAddr);
135
136} // namespace i2c