#include <bsp_i2c.hpp>
I2C device interface for configuration and register operations.
Definition at line 18 of file bsp_i2c.hpp.
◆ I2cDevice()
I2cDevice::I2cDevice |
( |
const std::string & |
device, |
|
|
uint8_t |
addr |
|
) |
| |
|
inline |
Opens and configures the I2C device.
- Parameters
-
device | I2C device file path (e.g., "/dev/i2c-1") |
addr | 7-bit I2C device address |
Definition at line 26 of file bsp_i2c.hpp.
27 : fd_(open(device.c_str(), O_RDWR)), addr_(addr) {
28 assert(!device.empty());
29
30 if (fd_ < 0) {
31 std::perror("Failed to open I2C device");
32 }
33
34 if (ioctl(fd_, I2C_SLAVE, addr_) < 0) {
35 close(fd_);
36 std::perror("Failed to configure I2C address");
37 }
38 }
◆ ~I2cDevice()
I2cDevice::~I2cDevice |
( |
| ) |
|
|
inline |
Closes the I2C device.
Definition at line 41 of file bsp_i2c.hpp.
41 {
42 if (fd_ >= 0) {
43 close(fd_);
44 }
45 }
◆ Address()
uint8_t I2cDevice::Address |
( |
| ) |
const |
|
inline |
Returns the I2C slave address.
Definition at line 120 of file bsp_i2c.hpp.
◆ Fd()
int I2cDevice::Fd |
( |
| ) |
const |
|
inline |
Returns the underlying file descriptor.
Definition at line 115 of file bsp_i2c.hpp.
◆ ReadRegister()
uint8_t I2cDevice::ReadRegister |
( |
uint8_t |
reg | ) |
|
|
inline |
Reads a single byte from an I2C register.
- Parameters
-
- Returns
- Value read
Definition at line 53 of file bsp_i2c.hpp.
53 {
54 if (write(fd_, ®, 1) != 1) {
55 std::perror("I2C write (set register) failed");
56 }
57
58 uint8_t value = 0;
59 if (read(fd_, &value, 1) != 1) {
60 std::perror("I2C read failed");
61 }
62
63 return value;
64 }
◆ ReadRegisters()
void I2cDevice::ReadRegisters |
( |
uint8_t |
reg, |
|
|
uint8_t * |
buffer, |
|
|
size_t |
length |
|
) |
| |
|
inline |
Reads multiple bytes starting from a given register.
- Parameters
-
reg | Starting register address |
buffer | Destination buffer |
length | Number of bytes to read |
Definition at line 86 of file bsp_i2c.hpp.
86 {
87 assert(buffer);
88
89 if (write(fd_, ®, 1) != 1) {
90 std::perror("I2C write (set start register) failed");
91 }
92
93 if (read(fd_, buffer, length) != static_cast<ssize_t>(length)) {
94 std::perror("I2C multi-byte read failed");
95 }
96 }
◆ WriteRaw()
void I2cDevice::WriteRaw |
( |
const uint8_t * |
data, |
|
|
size_t |
length |
|
) |
| |
|
inline |
Writes raw bytes directly to the I2C device without a register prefix.
- Parameters
-
data | Pointer to raw data buffer |
length | Number of bytes to write |
Definition at line 104 of file bsp_i2c.hpp.
104 {
105 assert(data);
106
107 if (write(fd_, data, length) != static_cast<ssize_t>(length)) {
108 std::perror("I2C raw write failed");
109 }
110 }
◆ WriteRegister()
void I2cDevice::WriteRegister |
( |
uint8_t |
reg, |
|
|
uint8_t |
value |
|
) |
| |
|
inline |
Writes a single byte to an I2C register.
- Parameters
-
reg | Register address |
value | Value to write |
Definition at line 72 of file bsp_i2c.hpp.
72 {
73 uint8_t buf[2] = {reg, value};
74 if (write(fd_, buf, 2) != 2) {
75 std::perror("I2C write failed");
76 }
77 }
◆ addr_
◆ fd_
The documentation for this class was generated from the following file: