51 #include "rnr/rnrconfig.h" 69 const char *devNames[] = { LaeDevI2C_0, LaeDevI2C_1, LaeDevI2C_2, NULL };
73 for(
int i = 0; devNames[i] != NULL; ++i)
75 LOGDIAG2(
"Try I2C device %s.", devNames[i]);
77 if( (rc = i2cBus.
open(devNames[i])) == LAE_OK )
79 if( i2cBus.
check(addr) )
81 LOGDIAG2(
"Address 0x%02x found on I2C device %s.", addr, devNames[i]);
86 LOGDIAG2(
"No address 0x%02x found on I2C device %s.",
92 rc = -LAE_ECODE_NO_DEV;
105 pthread_mutex_init(&
m_mutex, NULL);
111 pthread_mutex_destroy(&
m_mutex);
116 string strDevNameReal;
124 if( i2c_open(&
m_i2c, strDevNameReal.c_str()) < 0 )
126 LOGSYSERROR(
"%s.", strDevNameReal.c_str());
134 LOGDIAG2(
"I2C device %s opened.",
m_strDevName.c_str());
151 LOGDIAG3(
"I2C device %s closed.",
m_strDevName.c_str());
164 return i2c_exists(&
m_i2c, (i2c_addr_t)addr) == 1;
173 if( (n = i2c_read(&
m_i2c, (i2c_addr_t)addr, buf, (uint_t)len)) < 0 )
176 LOGDIAG3(
"I2C: Slave device address 0x%02x: " 177 "Failed to read from endpoint device data.",
182 else if( n != (
int)len )
184 LOGWARN(
"I2C: Slave device address 0x%02x: " 185 "Only %d/%d bytes read from endpoint device.",
200 if( (n = i2c_write(&
m_i2c, (i2c_addr_t)addr, buf, (uint_t)len)) < 0 )
203 LOGDIAG3(
"I2C: Slave device address 0x%02x: " 204 "Failed to write to device data.",
209 else if( n != (
int)len )
211 LOGWARN(
"I2C: Slave device address 0x%02x: " 212 "Only %d/%d bytes written to endpoint device.",
222 byte_t rbuf[],
size_t rlen)
228 rc = i2c_transfer(&
m_i2c, (i2c_addr_t)addr, wbuf, (uint_t)wlen,
233 LOGSYSERROR(
"I2C: Slave device address 0x%02x: " 234 "Failed to perform write/read transaction.",
250 byte_t rbuf[],
size_t rlen,
263 if( (n = i2c_write(&
m_i2c, (i2c_addr_t)addr, wbuf, (uint_t)wlen)) < 0 )
266 LOGDIAG3(
"I2C: Slave device address 0x%02x: " 267 "Failed to write to device data.",
272 else if( n != (
int)wlen )
274 LOGERROR(
"I2C: Slave device address 0x%02x: " 275 "Only %d/%d bytes written to endpoint device.",
284 if( (rlen > 0) && (rc ==
LAE_OK) )
292 if( (n = i2c_read(&
m_i2c, (i2c_addr_t)addr, rbuf, (uint_t)rlen)) < 0 )
295 LOGDIAG3(
"I2C: Slave device address 0x%02x: " 296 "Failed to read from endpoint device data.",
301 else if( n != (
int)rlen )
303 LOGERROR(
"I2C: Slave device address 0x%02x: " 304 "Only %d/%d bytes read from endpoint device.",
void unlock()
Unlock the I2C bus.
std::string m_strDevName
I2C device name.
virtual int write_read(uint_t addr, const byte_t wbuf[], size_t wlen, byte_t rbuf[], size_t rlen, long usec=0)
Perform a write/read transaction to/from an I2C slave endpoint device.
virtual int transfer(uint_t addr, const byte_t wbuf[], size_t wlen, byte_t rbuf[], size_t rlen)
Perform a write/read transfer to/from an I2C slave endpoint device.
i2c_t m_i2c
I2C bus instance.
static const int LAE_ECODE_IO
I/O error.
pthread_mutex_t m_mutex
mutual exclusion
Laelaps I2C class interface.
virtual ~LaeI2C()
Destructor.
virtual int open(const std::string &strDevName)
Open I2C bus device.
virtual int read(uint_t addr, byte_t buf[], size_t len)
Read from a I2C slave endpoint device.
void lock()
Lock the I2C bus.
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
virtual int write(uint_t addr, const byte_t buf[], size_t len)
Write to an I2C slave endpoint device.
Laelaps common utilities.
static const int LAE_ECODE_NO_DEV
device not found error
static i2c_t I2CBusNone
No bound I2C bus.
virtual int close()
Close I2C bus device.
int i2cTryOpen(LaeI2C &i2cBus, uint_t addr)
Try to open a series of I2C devices to fine the required endpoint.
std::string getRealDeviceName(const std::string &strDevName)
Get real device name.
virtual bool check(uint_t addr)
Check if an I2C slave endpoint exists.
Top-level package include file.
static const int LAE_OK
not an error, success