Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laeI2C.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Laelaps
4 //
5 // File: laeI2C.h
6 //
7 /*! \file
8  *
9  * \brief Laelaps I2C class interface.
10  *
11  * The I2C class supports safe multi-threading.
12  *
13  * \author Robin Knight (robin.knight@roadnarrows.com)
14  *
15  * \par Copyright
16  * \h_copy 2015-2017. RoadNarrows LLC.\n
17  * http://www.roadnarrows.com\n
18  * All Rights Reserved
19  */
20 /*
21  * @EulaBegin@
22  *
23  * Unless otherwise stated explicitly, all materials contained are copyrighted
24  * and may not be used without RoadNarrows LLC's written consent,
25  * except as provided in these terms and conditions or in the copyright
26  * notice (documents and software) or other proprietary notice provided with
27  * the relevant materials.
28  *
29  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
30  * MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
31  * BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
32  * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
33  * DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
34  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
39  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
40  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41  *
42  * @EulaEnd@
43  */
44 ////////////////////////////////////////////////////////////////////////////////
45 
46 #ifndef _LAE_I2C_H
47 #define _LAE_I2C_H
48 
49 #include <sys/types.h>
50 #include <pthread.h>
51 
52 #include <string>
53 
54 #include "rnr/rnrconfig.h"
55 #include "rnr/i2c.h"
56 #include "rnr/log.h"
57 
58 #include "Laelaps/laelaps.h"
59 
60 /*!
61  * \brief The \h_laelaps namespace encapsulates all \h_laelaps related
62  * constructs.
63  */
64 #ifndef SWIG
65 namespace laelaps
66 {
67 #endif // SWIG
68 
69  //----------------------------------------------------------------------------
70  // LaeI2C Class
71  //----------------------------------------------------------------------------
72 
73  /*!
74  * I2C class.
75  */
76  class LaeI2C
77  {
78  public:
79  /*!
80  * \brief Default constructor.
81  */
82  LaeI2C();
83 
84  /*!
85  * \brief Destructor.
86  */
87  virtual ~LaeI2C();
88 
89  /*!
90  * \brief Open \h_i2c bus device.
91  *
92  * \param strDevName \h_i2c device name.
93  *
94  * \copydoc doc_return_std
95  */
96  virtual int open(const std::string &strDevName);
97 
98  /*!
99  * \brief Close \h_i2c bus device.
100  *
101  * \copydoc doc_return_std
102  */
103  virtual int close();
104 
105  /*!
106  * \brief Check if an \h_i2c slave endpoint exists.
107  *
108  * \param addr Endpoint \h_i2c device's 7/10-bit address.
109  *
110  * \return Returns true or false.
111  */
112  virtual bool check(uint_t addr);
113 
114  /*!
115  * \brief Read from a \h_i2c slave endpoint device.
116  *
117  * Reads data from a device at the given address on the bound \h_i2c bus.
118  *
119  * \param addr Endpoint \h_i2c device's 7/10-bit address.
120  * \param [out] buf Pointer to the buffer that will receive the data bytes.
121  * \param len Number of bytes to read.
122  *
123  * \return
124  * On success, returns \h_ge 0 number of bytes read.\n
125  * Else returns \h_lt 0 error code.
126  */
127  virtual int read(uint_t addr, byte_t buf[], size_t len);
128 
129  /*!
130  * \brief Write to an \h_i2c slave endpoint device.
131  *
132  * Writes data to a device at the given address on the bound \h_i2c bus.
133  *
134  * \param addr Endpoint \h_i2c device's 7/10-bit address.
135  * \param [in] buf Pointer to the buffer that will be written.
136  * \param len Number of bytes to write.
137  *
138  * \return
139  * On success, returns \h_ge 0 number of bytes written.\n
140  * Else returns \h_lt 0 error code.
141  */
142  virtual int write(uint_t addr, const byte_t buf[], size_t len);
143 
144  /*!
145  * \brief Perform a write/read transfer to/from an \h_i2c slave endpoint
146  * device.
147  *
148  * An \h_i2c tranfer sends/receives one or more messages before the STOP
149  * is issued to terminate the operation. Each message does begin with a
150  * START.
151  *
152  * \note Not all slave devices support this fast transfer protocol. If not,
153  * then use the \ref write_read() instead.
154  *
155  * \param addr Endpoint \h_i2c device's 7/10-bit address.
156  * \param [in] wbuf Pointer to the buffer that contains the data
157  * to be written.
158  * \param wlen Number of bytes to write.
159  * \param [out] rbuf Pointer to the buffer that will receive the data.
160  * \param rlen Number of bytes to read.
161  *
162  * \copydoc doc_return_std
163  */
164  virtual int transfer(uint_t addr, const byte_t wbuf[], size_t wlen,
165  byte_t rbuf[], size_t rlen);
166 
167  /*!
168  * \brief Perform a write/read transaction to/from an \h_i2c slave endpoint
169  * device.
170  *
171  * Before each message a START is issued.
172  * After each message, a STOP is issued.
173  *
174  * \param addr Endpoint \h_i2c device's 7/10-bit address.
175  * \param [in] wbuf Pointer to the buffer that contains the data
176  * to be written.
177  * \param wlen Number of bytes to write.
178  * \param [out] rbuf Pointer to the buffer that will receive the data.
179  * \param rlen Number of bytes to read.
180  * \param usec Delay between write and read operations (usecs).
181  *
182  * \copydoc doc_return_std
183  */
184  virtual int write_read(uint_t addr, const byte_t wbuf[], size_t wlen,
185  byte_t rbuf[], size_t rlen,
186  long usec=0);
187 
188  /*!
189  * \brief Get associated \h_i2c device name.
190  *
191  * \return Returns device name string. Empty if no device.
192  */
193  std::string getDevName()
194  {
195  return m_strDevName;
196  }
197 
198  /*!
199  * \brief Check if device is open.
200  *
201  * \return Returns true or false.
202  */
203  bool isOpen()
204  {
205  return m_i2c.fd >= 0? true: false;
206  }
207 
208  protected:
209  std::string m_strDevName; ///< \h_i2c device name
210  i2c_t m_i2c; ///< \h_i2c bus instance
211  pthread_mutex_t m_mutex; ///< mutual exclusion
212 
213  /*!
214  * \brief Lock the \h_i2c bus.
215  *
216  * The lock()/unlock() primitives provide safe multi-threading access.
217  *
218  * \par Context:
219  * Any.
220  */
221  void lock()
222  {
223  pthread_mutex_lock(&m_mutex);
224  }
225 
226 
227  /*!
228  * \brief Unlock the \h_i2c bus.
229  *
230  * The lock()/unlock() primitives provide safe multi-threading access.
231  *
232  * \par Context:
233  * Any.
234  */
235  void unlock()
236  {
237  pthread_mutex_unlock(&m_mutex);
238  }
239 
240  }; // class LaeI2C
241 
242 
243  //----------------------------------------------------------------------------
244  // LaeI2C Utilities
245  //----------------------------------------------------------------------------
246 
247  /*!
248  * \brief Try to open a series of \h_i2c devices to fine the required
249  * endpoint.
250  *
251  * If the enpoint does not exist, the device is closed and the next device
252  * is tried.
253  *
254  * On success, the device bus is left open.
255  *
256  * \param i2cBus The \h_i2c bus.
257  * \param addr Endpoint \h_i2c device's 7/10-bit address.
258  *
259  * \copydoc doc_return_std
260  */
261  int i2cTryOpen(LaeI2C &i2cBus, uint_t addr);
262 
263 #ifndef SWIG
264 } // namespace laelaps
265 #endif // SWIG
266 
267 
268 #endif // _LAE_I2C_H
void unlock()
Unlock the I2C bus.
Definition: laeI2C.h:235
std::string m_strDevName
I2C device name.
Definition: laeI2C.h:209
std::string getDevName()
Get associated I2C device name.
Definition: laeI2C.h:193
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
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.
Definition: laeI2C.cxx:221
i2c_t m_i2c
I2C bus instance.
Definition: laeI2C.h:210
pthread_mutex_t m_mutex
mutual exclusion
Definition: laeI2C.h:211
LaeI2C()
Default constructor.
Definition: laeI2C.cxx:102
virtual ~LaeI2C()
Destructor.
Definition: laeI2C.cxx:108
virtual int open(const std::string &strDevName)
Open I2C bus device.
Definition: laeI2C.cxx:114
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 I2C bus.
Definition: laeI2C.h:221
The <b><i>Laelaps</i></b> namespace encapsulates all <b><i>Laelaps</i></b> related constructs...
Definition: laeAlarms.h:64
virtual int write(uint_t addr, const byte_t buf[], size_t len)
Write to an I2C slave endpoint device.
Definition: laeI2C.cxx:194
bool isOpen()
Check if device is open.
Definition: laeI2C.h:203
virtual int close()
Close I2C bus device.
Definition: laeI2C.cxx:144
int i2cTryOpen(LaeI2C &i2cBus, uint_t addr)
Try to open a series of I2C devices to fine the required endpoint.
Definition: laeI2C.cxx:67
virtual bool check(uint_t addr)
Check if an I2C slave endpoint exists.
Definition: laeI2C.cxx:162
Top-level package include file.