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

control register mask More...

#include <laeI2CMux.h>

Public Member Functions

 LaeI2CMux (LaeI2C &i2cBus, uint_t addr=LaeI2CMuxAddrDft)
 Initialization constructor. More...
 
virtual ~LaeI2CMux ()
 Destructor.
 
virtual int read (int chan, uint_t addr, byte_t buf[], size_t len)
 Read from a multiplexed I2C slave endpoint device. More...
 
virtual int write (int chan, uint_t addr, byte_t buf[], size_t len)
 Write from an I2C endpoint device. More...
 
virtual int transact (int chan, 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...
 
virtual int readChannelStates (byte_t &chanBits)
 Read the current I2C multiplexer enable state. More...
 
virtual int reset ()
 Reset I2C devices on multiplexer bus. More...
 
bool isChanEnabled (int chan, byte_t chanBits)
 Check if a channel is enabled in the channel bit map. More...
 
std::string getDevName ()
 Get associated I2C device name. More...
 
bool isOpen ()
 Check if bound I2C device is open. More...
 

Protected Member Functions

void lock ()
 Lock the shared resource. More...
 
void unlock ()
 Unlock the shared resource. More...
 
int setChannel (int chan)
 Set channel. More...
 

Protected Attributes

LaeI2Cm_i2cBus
 boulnd I2C bus instance
 
uint_t m_addrMux
 I2C multiplexer address.
 
int m_chan
 current active channel
 
pthread_mutex_t m_mutex
 mutex
 

Detailed Description

control register mask

TI PCA9548A I2C mulitplexer switch class.

Definition at line 100 of file laeI2CMux.h.

Constructor & Destructor Documentation

LaeI2CMux::LaeI2CMux ( LaeI2C i2cBus,
uint_t  addr = LaeI2CMuxAddrDft 
)

Initialization constructor.

Parameters
i2cbusBound open I2C bus instance.
addrI2C Mux address.

Definition at line 63 of file laeI2CMux.cxx.

References laelaps::LaeI2CMuxChanNone, m_chan, and m_mutex.

63  :
64  m_i2cBus(i2cBus), m_addrMux(addr)
65 {
67 
68  pthread_mutex_init(&m_mutex, NULL);
69 }
uint_t m_addrMux
I2C multiplexer address.
Definition: laeI2CMux.h:223
const int LaeI2CMuxChanNone
no channel selected
Definition: laeI2CMux.h:86
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
pthread_mutex_t m_mutex
mutex
Definition: laeI2CMux.h:227
int m_chan
current active channel
Definition: laeI2CMux.h:224

Member Function Documentation

std::string laelaps::LaeI2CMux::getDevName ( )
inline

Get associated I2C device name.

Returns
Returns device name string. Empty if no device.

Definition at line 206 of file laeI2CMux.h.

References laelaps::LaeI2C::getDevName(), and m_i2cBus.

207  {
208  return m_i2cBus.getDevName();
209  }
std::string getDevName()
Get associated I2C device name.
Definition: laeI2C.h:193
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
bool laelaps::LaeI2CMux::isChanEnabled ( int  chan,
byte_t  chanBits 
)
inline

Check if a channel is enabled in the channel bit map.

Parameters
chanMultiplexed channel number.
chanBitsBit map of enable/disable channels.
Returns
True or false.

Definition at line 196 of file laeI2CMux.h.

197  {
198  return (LaeI2CMuxCtlRegMask & ((byte_t)(1 << chan) & chanBits)) != 0;
199  }
void laelaps::LaeI2CMux::lock ( )
inlineprotected

Lock the shared resource.

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

Context:
Any.

Definition at line 237 of file laeI2CMux.h.

Referenced by read(), readChannelStates(), transact(), and write().

238  {
239  pthread_mutex_lock(&m_mutex);
240  }
pthread_mutex_t m_mutex
mutex
Definition: laeI2CMux.h:227
int LaeI2CMux::read ( int  chan,
uint_t  addr,
byte_t  buf[],
size_t  len 
)
virtual

Read from a multiplexed I2C slave endpoint device.

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

Parameters
chanMultiplexed channel number.
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 76 of file laeI2CMux.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, lock(), m_chan, m_i2cBus, laelaps::LaeI2C::read(), setChannel(), and unlock().

77 {
78  int n;
79  int rc;
80 
81  lock();
82 
83  if( (rc = setChannel(chan)) == LAE_OK )
84  {
85  if( (n = m_i2cBus.read(addr, buf, len)) < 0 )
86  {
87  LOGDIAG3("I2C Mux: Channel %d I2C device address 0x%02x: "
88  "Failed to read from endpoint device data.",
89  m_chan, addr);
90  rc = -LAE_ECODE_IO;
91  }
92 
93  if( n != (int)len )
94  {
95  LOGWARN("I2C Mux: Channel %d I2C device address 0x%02x: "
96  "Only %d/%d bytes read from endpoint device.",
97  m_chan, addr, n, (int)len);
98  }
99  }
100 
101  unlock();
102 
103  return rc < 0? rc: n;
104 }
int setChannel(int chan)
Set channel.
Definition: laeI2CMux.cxx:185
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
virtual int read(uint_t addr, byte_t buf[], size_t len)
Read from a I2C slave endpoint device.
Definition: laeI2C.cxx:167
int m_chan
current active channel
Definition: laeI2CMux.h:224
void lock()
Lock the shared resource.
Definition: laeI2CMux.h:237
void unlock()
Unlock the shared resource.
Definition: laeI2CMux.h:248
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
int LaeI2CMux::readChannelStates ( byte_t &  chanBits)
virtual

Read the current I2C multiplexer enable state.

Parameters
[out]chanBitsBit map of enable/disable channels.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 163 of file laeI2CMux.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, lock(), m_addrMux, m_i2cBus, laelaps::LaeI2C::read(), and unlock().

164 {
165  int rc;
166 
167  lock();
168 
169  if( m_i2cBus.read(m_addrMux, &chanBits, 1) < 0 )
170  {
171  LOGSYSERROR("I2C Mux: Address 0x%02x: Failed to read channel states.",
172  m_addrMux);
173  rc = -LAE_ECODE_IO;
174  }
175  else
176  {
177  rc = LAE_OK;
178  }
179 
180  unlock();
181 
182  return rc;
183 }
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
uint_t m_addrMux
I2C multiplexer address.
Definition: laeI2CMux.h:223
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
virtual int read(uint_t addr, byte_t buf[], size_t len)
Read from a I2C slave endpoint device.
Definition: laeI2C.cxx:167
void lock()
Lock the shared resource.
Definition: laeI2CMux.h:237
void unlock()
Unlock the shared resource.
Definition: laeI2CMux.h:248
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
virtual int laelaps::LaeI2CMux::reset ( )
inlinevirtual

Reset I2C devices on multiplexer bus.

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

Definition at line 182 of file laeI2CMux.h.

References laelaps::LAE_ECODE_INTERNAL.

183  {
184  // TODO
185  return -LAE_ECODE_INTERNAL;
186  }
static const int LAE_ECODE_INTERNAL
internal error (bug)
Definition: laelaps.h:75
int LaeI2CMux::setChannel ( int  chan)
protected

Set channel.

This function does not call the lock/unlock mutex.

Parameters
chanMultiplexed channel number.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Definition at line 185 of file laeI2CMux.cxx.

References laelaps::LAE_ECODE_BAD_VAL, laelaps::LAE_ECODE_IO, laelaps::LAE_OK, laelaps::LaeI2CMuxChanMax, laelaps::LaeI2CMuxChanMin, m_addrMux, m_chan, m_i2cBus, and laelaps::LaeI2C::write().

Referenced by read(), transact(), unlock(), and write().

186 {
187  if( (chan < LaeI2CMuxChanMin) || (chan > LaeI2CMuxChanMax) )
188  {
189  LOGERROR("I2C Mux: %d channel: Out-of-range.", chan);
190  return -LAE_ECODE_BAD_VAL;
191  }
192 
193  else if( m_chan != chan )
194  {
195  byte_t ctl = (byte_t)(1 << chan); // control register data
196 
197  if( m_i2cBus.write(m_addrMux, &ctl, 1) < 0 )
198  {
199  LOGSYSERROR("I2C Mux: Address 0x%02x: Failed to select channel.",
200  m_addrMux);
201  return -LAE_ECODE_IO;
202  }
203  else
204  {
205  m_chan = chan;
206  usleep(100);
207  }
208  }
209 
210  return LAE_OK;
211 }
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
uint_t m_addrMux
I2C multiplexer address.
Definition: laeI2CMux.h:223
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
virtual int write(uint_t addr, const byte_t buf[], size_t len)
Write to an I2C slave endpoint device.
Definition: laeI2C.cxx:194
const int LaeI2CMuxChanMax
min channel number
Definition: laeI2CMux.h:85
int m_chan
current active channel
Definition: laeI2CMux.h:224
const int LaeI2CMuxChanMin
min channel number
Definition: laeI2CMux.h:84
static const int LAE_ECODE_BAD_VAL
bad value general error
Definition: laelaps.h:76
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
int LaeI2CMux::transact ( int  chan,
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.

Parameters
chanMultiplexed channel number.
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 136 of file laeI2CMux.cxx.

References laelaps::LAE_OK, lock(), m_chan, m_i2cBus, setChannel(), unlock(), and laelaps::LaeI2C::write_read().

Referenced by sensor::vl6180::LaeVL6180Mux::readReg16(), and sensor::vl6180::LaeVL6180Mux::readReg8().

140 {
141  int n;
142  int rc;
143 
144  lock();
145 
146  if( (rc = setChannel(chan)) == LAE_OK )
147  {
148  rc = m_i2cBus.write_read(addr, wbuf, wlen, rbuf, rlen, usec);
149 
150  if( rc != LAE_OK )
151  {
152  LOGDIAG3("I2C Mux: Channel %d I2C device address 0x%02x: "
153  "Failed to write_read to device data.",
154  m_chan, addr);
155  }
156  }
157 
158  unlock();
159 
160  return rc < 0? rc: n;
161 }
int setChannel(int chan)
Set channel.
Definition: laeI2CMux.cxx:185
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.
Definition: laeI2C.cxx:249
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
int m_chan
current active channel
Definition: laeI2CMux.h:224
void lock()
Lock the shared resource.
Definition: laeI2CMux.h:237
void unlock()
Unlock the shared resource.
Definition: laeI2CMux.h:248
static const int LAE_OK
not an error, success
Definition: laelaps.h:71
void laelaps::LaeI2CMux::unlock ( )
inlineprotected

Unlock the shared resource.

Context:
Any.

Definition at line 248 of file laeI2CMux.h.

References setChannel().

Referenced by read(), readChannelStates(), transact(), and write().

249  {
250  pthread_mutex_unlock(&m_mutex);
251  }
pthread_mutex_t m_mutex
mutex
Definition: laeI2CMux.h:227
int LaeI2CMux::write ( int  chan,
uint_t  addr,
byte_t  buf[],
size_t  len 
)
virtual

Write from an I2C endpoint device.

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

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

Definition at line 106 of file laeI2CMux.cxx.

References laelaps::LAE_ECODE_IO, laelaps::LAE_OK, lock(), m_chan, m_i2cBus, setChannel(), unlock(), and laelaps::LaeI2C::write().

Referenced by sensor::vl6180::LaeVL6180Mux::writeReg16(), and sensor::vl6180::LaeVL6180Mux::writeReg8().

107 {
108  int n;
109  int rc;
110 
111  lock();
112 
113  if( (rc = setChannel(chan)) == LAE_OK )
114  {
115  if( (n = m_i2cBus.write(addr, buf, len)) < 0 )
116  {
117  LOGDIAG3("I2C Mux: Channel %d I2C device address 0x%02x: "
118  "Failed to write to device data.",
119  m_chan, addr);
120  rc = -LAE_ECODE_IO;
121  }
122 
123  else if( n != (int)len )
124  {
125  LOGWARN("I2C Mux: Channel %d I2C device address 0x%02x: "
126  "Only %d/%d bytes written to endpoint device.",
127  m_chan, addr, n, (int)len);
128  }
129  }
130 
131  unlock();
132 
133  return rc < 0? rc: n;
134 }
int setChannel(int chan)
Set channel.
Definition: laeI2CMux.cxx:185
static const int LAE_ECODE_IO
I/O error.
Definition: laelaps.h:98
LaeI2C & m_i2cBus
boulnd I2C bus instance
Definition: laeI2CMux.h:222
virtual int write(uint_t addr, const byte_t buf[], size_t len)
Write to an I2C slave endpoint device.
Definition: laeI2C.cxx:194
int m_chan
current active channel
Definition: laeI2CMux.h:224
void lock()
Lock the shared resource.
Definition: laeI2CMux.h:237
void unlock()
Unlock the shared resource.
Definition: laeI2CMux.h:248
static const int LAE_OK
not an error, success
Definition: laelaps.h:71

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