Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laelaps::LaeI2C Class Reference

#include <laeI2C.h>

Public Member Functions

 LaeI2C ()
 Default constructor.
 
virtual ~LaeI2C ()
 Destructor.
 
virtual int open (const std::string &strDevName)
 Open I2C bus device. More...
 
virtual int close ()
 Close I2C bus device. More...
 
virtual bool check (uint_t addr)
 Check if an I2C slave endpoint exists. More...
 
virtual int read (uint_t addr, byte_t buf[], size_t len)
 Read from a I2C slave endpoint device. More...
 
virtual int write (uint_t addr, const byte_t buf[], size_t len)
 Write to an I2C slave endpoint device. More...
 
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. More...
 
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. More...
 
std::string getDevName ()
 Get associated I2C device name. More...
 
bool isOpen ()
 Check if device is open. More...
 

Protected Member Functions

void lock ()
 Lock the I2C bus. More...
 
void unlock ()
 Unlock the I2C bus. More...
 

Protected Attributes

std::string m_strDevName
 I2C device name.
 
i2c_t m_i2c
 I2C bus instance.
 
pthread_mutex_t m_mutex
 mutual exclusion
 

Detailed Description

I2C class.

Definition at line 76 of file laeI2C.h.

Member Function Documentation

bool LaeI2C::check ( uint_t  addr)
virtual

Check if an I2C slave endpoint exists.

Parameters
addrEndpoint I2C device's 7/10-bit address.
Returns
Returns true or false.

Definition at line 162 of file laeI2C.cxx.

References m_i2c.

Referenced by laelaps::i2cTryOpen().

163 {
164  return i2c_exists(&m_i2c, (i2c_addr_t)addr) == 1;
165 }
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
int LaeI2C::close ( )
virtual

Close I2C bus device.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 144 of file laeI2C.cxx.

References I2CBusNone, laelaps::LAE_OK, lock(), m_i2c, m_strDevName, and unlock().

Referenced by Laelaps.WatchDog.WatchDog::attach(), laelaps::LaeRobot::disconnect(), laelaps::i2cTryOpen(), main(), Laelaps.WatchDog.WatchDog::open(), and ~LaeI2C().

145 {
146  lock();
147 
148  if( m_i2c.fd >= 0 )
149  {
150  i2c_close(&m_i2c);
151  LOGDIAG3("I2C device %s closed.", m_strDevName.c_str());
152  }
153 
154  m_strDevName.clear();
155  m_i2c = I2CBusNone;
156 
157  unlock();
158 
159  return LAE_OK;
160 }
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
std::string m_strDevName
I2C device name.
Definition: laeI2C.h:209
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
void lock()
Lock the I2C bus.
Definition: laeI2C.h:221
static i2c_t I2CBusNone
No bound I2C bus.
Definition: laeI2C.cxx:100
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
std::string laelaps::LaeI2C::getDevName ( )
inline

Get associated I2C device name.

Returns
Returns device name string. Empty if no device.

Definition at line 193 of file laeI2C.h.

References m_strDevName.

Referenced by laelaps::LaeI2CMux::getDevName().

194  {
195  return m_strDevName;
196  }
std::string m_strDevName
I2C device name.
Definition: laeI2C.h:209
void laelaps::LaeI2C::lock ( )
inlineprotected

Lock the I2C bus.

The lock()/unlock() primitives provide safe multi-threading access.

Context:
Any.

Definition at line 221 of file laeI2C.h.

Referenced by close(), open(), read(), transfer(), write(), and write_read().

222  {
223  pthread_mutex_lock(&m_mutex);
224  }
pthread_mutex_t m_mutex
mutual exclusion
Definition: laeI2C.h:211
int LaeI2C::open ( const std::string &  strDevName)
virtual

Open I2C bus device.

Parameters
strDevNameI2C device name.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 114 of file laeI2C.cxx.

References laelaps::getRealDeviceName(), laelaps::LAE_ECODE_NO_DEV, laelaps::LAE_OK, lock(), m_i2c, m_strDevName, and unlock().

Referenced by laelaps::i2cTryOpen().

115 {
116  string strDevNameReal;
117  int rc;
118 
119  lock();
120 
121  // Get the real device name, not any symbolic links.
122  strDevNameReal = getRealDeviceName(strDevName);
123 
124  if( i2c_open(&m_i2c, strDevNameReal.c_str()) < 0 )
125  {
126  LOGSYSERROR("%s.", strDevNameReal.c_str());
127  rc = -LAE_ECODE_NO_DEV;
128  }
129 
130  else
131  {
132  m_strDevName = strDevNameReal;
133 
134  LOGDIAG2("I2C device %s opened.", m_strDevName.c_str());
135 
136  rc = LAE_OK;
137  }
138 
139  unlock();
140 
141  return rc;
142 }
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
std::string m_strDevName
I2C device name.
Definition: laeI2C.h:209
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
void lock()
Lock the I2C bus.
Definition: laeI2C.h:221
static const int LAE_ECODE_NO_DEV
device not found error
Definition: laelaps.h:82
std::string getRealDeviceName(const std::string &strDevName)
Get real device name.
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
int LaeI2C::read ( uint_t  addr,
byte_t  buf[],
size_t  len 
)
virtual

Read from a I2C slave endpoint device.

Reads data from a device at the given address on the bound I2C bus.

Parameters
addrEndpoint I2C device's 7/10-bit address.
[out]bufPointer to the buffer that will receive the data bytes.
lenNumber of bytes to read.
Returns
On success, returns ≥ 0 number of bytes read.
Else returns < 0 error code.

Definition at line 167 of file laeI2C.cxx.

References laelaps::LAE_ECODE_IO, lock(), m_i2c, and unlock().

Referenced by laelaps::LaeI2CMux::read(), and laelaps::LaeI2CMux::readChannelStates().

168 {
169  int n;
170 
171  lock();
172 
173  if( (n = i2c_read(&m_i2c, (i2c_addr_t)addr, buf, (uint_t)len)) < 0 )
174  {
175  // LOGSYSERROR
176  LOGDIAG3("I2C: Slave device address 0x%02x: "
177  "Failed to read from endpoint device data.",
178  addr);
179  n = -LAE_ECODE_IO;
180  }
181 
182  else if( n != (int)len )
183  {
184  LOGWARN("I2C: Slave device address 0x%02x: "
185  "Only %d/%d bytes read from endpoint device.",
186  addr, n, (int)len);
187  }
188 
189  unlock();
190 
191  return n;
192 }
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
void lock()
Lock the I2C bus.
Definition: laeI2C.h:221
int LaeI2C::transfer ( uint_t  addr,
const byte_t  wbuf[],
size_t  wlen,
byte_t  rbuf[],
size_t  rlen 
)
virtual

Perform a write/read transfer to/from an I2C slave endpoint device.

An I2C tranfer sends/receives one or more messages before the STOP is issued to terminate the operation. Each message does begin with a START.

Note
Not all slave devices support this fast transfer protocol. If not, then use the write_read() instead.
Parameters
addrEndpoint I2C device's 7/10-bit address.
[in]wbufPointer to the buffer that contains the data to be written.
wlenNumber of bytes to write.
[out]rbufPointer to the buffer that will receive the data.
rlenNumber of bytes to read.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 221 of file laeI2C.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, lock(), m_i2c, and unlock().

223 {
224  int rc;
225 
226  lock();
227 
228  rc = i2c_transfer(&m_i2c, (i2c_addr_t)addr, wbuf, (uint_t)wlen,
229  rbuf, (uint_t)rlen);
230 
231  if( rc < 0 )
232  {
233  LOGSYSERROR("I2C: Slave device address 0x%02x: "
234  "Failed to perform write/read transaction.",
235  addr);
236  rc = -LAE_ECODE_IO;
237  }
238 
239  else
240  {
241  rc = LAE_OK;
242  }
243 
244  unlock();
245 
246  return rc;
247 }
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
void lock()
Lock the I2C bus.
Definition: laeI2C.h:221
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
void laelaps::LaeI2C::unlock ( )
inlineprotected

Unlock the I2C bus.

The lock()/unlock() primitives provide safe multi-threading access.

Context:
Any.

Definition at line 235 of file laeI2C.h.

References laelaps::i2cTryOpen().

Referenced by close(), open(), read(), transfer(), write(), and write_read().

236  {
237  pthread_mutex_unlock(&m_mutex);
238  }
pthread_mutex_t m_mutex
mutual exclusion
Definition: laeI2C.h:211
int LaeI2C::write ( uint_t  addr,
const byte_t  buf[],
size_t  len 
)
virtual

Write to an I2C slave endpoint device.

Writes data to a device at the given address on the bound I2C bus.

Parameters
addrEndpoint I2C device's 7/10-bit address.
[in]bufPointer to the buffer that will be written.
lenNumber of bytes to write.
Returns
On success, returns ≥ 0 number of bytes written.
Else returns < 0 error code.

Definition at line 194 of file laeI2C.cxx.

References laelaps::LAE_ECODE_IO, lock(), m_i2c, and unlock().

Referenced by laelaps::LaeWd::cmdConfigDPin(), laelaps::LaeWd::cmdConfigOperation(), laelaps::LaeWd::cmdEnableAuxPort5V(), laelaps::LaeWd::cmdEnableAuxPortBatt(), laelaps::LaeWd::cmdPetTheDog(), laelaps::LaeWd::cmdResetRgbLed(), laelaps::LaeWd::cmdSetAlarms(), laelaps::LaeWd::cmdSetBatterySoC(), laelaps::LaeWd::cmdSetRgbLed(), laelaps::LaeWd::cmdWriteAPin(), laelaps::LaeWd::cmdWriteDPin(), laelaps::LaeI2CMux::setChannel(), and laelaps::LaeI2CMux::write().

195 {
196  int n;
197 
198  lock();
199 
200  if( (n = i2c_write(&m_i2c, (i2c_addr_t)addr, buf, (uint_t)len)) < 0 )
201  {
202  // LOGSYSERROR
203  LOGDIAG3("I2C: Slave device address 0x%02x: "
204  "Failed to write to device data.",
205  addr);
206  n = -LAE_ECODE_IO;
207  }
208 
209  else if( n != (int)len )
210  {
211  LOGWARN("I2C: Slave device address 0x%02x: "
212  "Only %d/%d bytes written to endpoint device.",
213  addr, n, (int)len);
214  }
215 
216  unlock();
217 
218  return n;
219 }
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
void lock()
Lock the I2C bus.
Definition: laeI2C.h:221
int LaeI2C::write_read ( uint_t  addr,
const byte_t  wbuf[],
size_t  wlen,
byte_t  rbuf[],
size_t  rlen,
long  usec = 0 
)
virtual

Perform a write/read transaction to/from an I2C slave endpoint device.

Before each message a START is issued. After each message, a STOP is issued.

Parameters
addrEndpoint I2C device's 7/10-bit address.
[in]wbufPointer to the buffer that contains the data to be written.
wlenNumber of bytes to write.
[out]rbufPointer to the buffer that will receive the data.
rlenNumber of bytes to read.
usecDelay between write and read operations (usecs).
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 249 of file laeI2C.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, lock(), m_i2c, and unlock().

Referenced by laelaps::LaeWd::cmdEnableMotorCtlrs(), sensor::vl6180::LaeRangeMuxSubproc::cmdGetAmbientLight(), laelaps::LaeWd::cmdGetFwVersion(), sensor::vl6180::LaeRangeMuxSubproc::cmdGetFwVersion(), sensor::vl6180::LaeRangeMuxSubproc::cmdGetIdent(), sensor::vl6180::LaeRangeMuxSubproc::cmdGetRanges(), sensor::vl6180::LaeRangeMuxSubproc::cmdGetTunes(), laelaps::LaeWd::cmdPetTheDog(), laelaps::LaeWd::cmdReadAPin(), laelaps::LaeWd::cmdReadDPin(), laelaps::LaeWd::cmdReadEnables(), laelaps::LaeWd::cmdReadVoltages(), laelaps::LaeWd::cmdTest(), sensor::vl6180::LaeRangeMuxSubproc::cmdTuneAls(), sensor::vl6180::LaeRangeMuxSubproc::cmdTuneToFSensor(), and laelaps::LaeI2CMux::transact().

252 {
253  int n;
254  int rc = LAE_OK;
255 
256  lock();
257 
258  //
259  // Write.
260  //
261  if( wlen > 0 )
262  {
263  if( (n = i2c_write(&m_i2c, (i2c_addr_t)addr, wbuf, (uint_t)wlen)) < 0 )
264  {
265  // LOGSYSERROR
266  LOGDIAG3("I2C: Slave device address 0x%02x: "
267  "Failed to write to device data.",
268  addr);
269  rc = -LAE_ECODE_IO;
270  }
271 
272  else if( n != (int)wlen )
273  {
274  LOGERROR("I2C: Slave device address 0x%02x: "
275  "Only %d/%d bytes written to endpoint device.",
276  addr, n, (int)wlen);
277  rc = -LAE_ECODE_IO;
278  }
279  }
280 
281  //
282  // Read.
283  //
284  if( (rlen > 0) && (rc == LAE_OK) )
285  {
286  // delay
287  if( usec > 0 )
288  {
289  usleep(usec);
290  }
291 
292  if( (n = i2c_read(&m_i2c, (i2c_addr_t)addr, rbuf, (uint_t)rlen)) < 0 )
293  {
294  // LOGSYSERROR
295  LOGDIAG3("I2C: Slave device address 0x%02x: "
296  "Failed to read from endpoint device data.",
297  addr);
298  rc = -LAE_ECODE_IO;
299  }
300 
301  else if( n != (int)rlen )
302  {
303  LOGERROR("I2C: Slave device address 0x%02x: "
304  "Only %d/%d bytes read from endpoint device.",
305  addr, n, (int)rlen);
306  rc = -LAE_ECODE_IO;
307  }
308  }
309 
310  unlock();
311 
312  return rc;
313 }
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
void lock()
Lock the I2C bus.
Definition: laeI2C.h:221
static const int LAE_OK
not an error, success
Definition: laelaps.h:71

The documentation for this class was generated from the following files: